結果

問題 No.198 キャンディー・ボックス2
ユーザー oxyshoweroxyshower
提出日時 2019-08-24 17:59:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 919 bytes
コンパイル時間 1,521 ms
コンパイル使用メモリ 164,612 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 13:45:06
合計ジャッジ時間 2,373 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 WA -
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 WA -
testcase_20 AC 2 ms
5,376 KB
testcase_21 WA -
testcase_22 AC 2 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 1 ms
5,376 KB
testcase_25 WA -
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long

#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif

signed main(){

  int b,n; cin >> b >> n;
  vector<int> a(n);
  bool check = true;
  for(int i = 0; i < n; i++){
    cin >> a[i];
    if(i > 0 && a[i-1] != a[i]) check = false;
  }
  if(check){
    cout << 0 << endl;
    return 0;
  }

  auto f = [&](int t){
    int cnt = 0;
    for(int i = 0; i < n; i++){
      cnt += abs(t - a[i]);
    }
    return cnt;
  };

  int left = 0,right = 1 << 30;
  //[left , t1 , t2 , right]
  for(int i = 0; i < 100; i++){
    int t1 = (left * 2 + right) / 3;
    int t2 = (left + right * 2) / 3;
    t2 += 1;

    if(f(t1) > f(t2)) left = t1;
    else right = t2;
  }

  int ans = 1 << 30;
  for(int i = left; i <= right; i++){
    ans = min(ans,f(i));
    if(ans == 0){
      cout << i << endl;
      return 0;
    }
  }
  cout << ans << endl;

  return 0;
}
0