結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,752 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 1,024 ms
5,312 KB
testcase_15 AC 1,082 ms
5,644 KB
testcase_16 AC 1,571 ms
7,088 KB
testcase_17 AC 1,379 ms
6,828 KB
testcase_18 AC 1,590 ms
8,180 KB
testcase_19 TLE -
testcase_20 TLE -
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,100){
      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,100){
      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