結果

問題 No.1391 ±1 Abs Sum
ユーザー どららどらら
提出日時 2021-02-12 23:09:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,120 bytes
コンパイル時間 1,619 ms
コンパイル使用メモリ 172,696 KB
実行使用メモリ 7,544 KB
最終ジャッジ日時 2023-09-27 06:29:00
合計ジャッジ時間 33,439 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,376 KB
testcase_10 WA -
testcase_11 AC 1,021 ms
7,544 KB
testcase_12 AC 968 ms
6,788 KB
testcase_13 AC 971 ms
7,128 KB
testcase_14 AC 396 ms
5,256 KB
testcase_15 AC 419 ms
5,288 KB
testcase_16 AC 602 ms
6,740 KB
testcase_17 AC 542 ms
6,652 KB
testcase_18 AC 714 ms
7,196 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1,835 ms
6,820 KB
testcase_22 AC 1,775 ms
7,048 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 WA -
testcase_28 AC 1,389 ms
5,640 KB
testcase_29 WA -
testcase_30 AC 1,441 ms
6,704 KB
testcase_31 AC 769 ms
7,456 KB
testcase_32 AC 368 ms
5,224 KB
testcase_33 AC 414 ms
5,312 KB
testcase_34 AC 1,914 ms
7,404 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 759 ms
7,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

ll f(ll x, const vector<int>& v, int K){
  vector<ll> ret;
  rep(i,v.size()){
    ret.push_back(abs(v[i]-x));
  }
  sort(ALLOF(ret));
  ll sum = 0;
  rep(i,v.size()){
    if(i<K) sum += ret[i];
    else sum += -ret[i];
  }
  return sum;
}

int main(){
  int N, K;
  cin >> N >> K;
  vector<int> v(N);
  rep(i,N) cin >> v[i];

  ll ret = f(v[N/2],v,K);
  ret = min(ret, f(v[0],v,K));
  ret = min(ret, f(v[N-1],v,K));

  {
    ll l = v[0], r = v[N/2];
    rep(i,30){
      if(f((l+l+r)/3,v,K)<f((l+r+r)/3,v,K)) r = (l+r+r)/3;
      else l = (l+l+r)/3;
    }
    ret = min(ret, f((l+r)/2,v,K));
  }
  {
    ll l = v[N/2], r = v[N-1];
    rep(i,30){
      if(f((l+l+r)/3,v,K)<f((l+r+r)/3,v,K)) r = (l+r+r)/3;
      else l = (l+l+r)/3;
    }
    ret = min(ret, f((l+r)/2,v,K));
  }

  cout << ret << endl;
  
  return 0;
}

0