結果

問題 No.198 キャンディー・ボックス2
ユーザー imulanimulan
提出日時 2016-02-04 00:54:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,047 bytes
コンパイル時間 3,889 ms
コンパイル使用メモリ 146,284 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-10 08:10:51
合計ジャッジ時間 2,337 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)
#define mp make_pair
#define pb push_back
#define fi first
#define sc second

ll ab(ll p){
  return (p>0)?p:-p;
}

int main(int argc, char const *argv[]) {
  long b;
  int n;
  cin >>b >>n;

  std::vector<ll> c(n);
  ll sum=0;
  rep(i,n){
    cin >>c[i];
    sum += c[i];
  }


  ll ans=sum;
  long left=0, right=(sum+b)/n;
  ll tmp=0;
  rep(i,n) tmp+=ab(c[i]-right);
  ans=min(ans,tmp);

  bool end=false;
  while (right>left) {
    //printf("  interval  %ld %ld\n",left,right);
    if(right-left<10) break;

    long xl=(left*2+right)/3;
    long xr=(left+right*2)/3;

    ll tl=0, tr=0;
    rep(i,n) tl+=ab(c[i]-xl);
    rep(i,n) tr+=ab(c[i]-xr);

    if(tl>=tr) left=xl;
    if(tl<=tr) right=xr;
  }

  for(long i=left; i<=right; ++i){
    ll tmp=0;
    rep(j,n) tmp+=ab(c[j]-i);
    ans=min(ans,tmp);
  }

  std::cout << ans << std::endl;
  return 0;
}
0