結果

問題 No.1391 ±1 Abs Sum
ユーザー どららどらら
提出日時 2021-02-12 23:08:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,121 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 171,824 KB
実行使用メモリ 11,720 KB
最終ジャッジ日時 2023-09-27 06:27:22
合計ジャッジ時間 17,634 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,884 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1,594 ms
7,472 KB
testcase_12 AC 1,577 ms
6,680 KB
testcase_13 AC 1,462 ms
7,172 KB
testcase_14 AC 602 ms
5,256 KB
testcase_15 AC 627 ms
5,536 KB
testcase_16 AC 917 ms
6,728 KB
testcase_17 AC 815 ms
6,744 KB
testcase_18 AC 1,078 ms
7,100 KB
testcase_19 AC 1,586 ms
6,612 KB
testcase_20 AC 1,238 ms
7,428 KB
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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,50){
      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,50){
      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