結果

問題 No.609 Noelちゃんと星々
ユーザー dustnn0dustnn0
提出日時 2018-03-10 22:45:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 610 bytes
コンパイル時間 1,972 ms
コンパイル使用メモリ 166,944 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 18:29:47
合計ジャッジ時間 4,008 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
5,248 KB
testcase_01 AC 25 ms
5,376 KB
testcase_02 AC 17 ms
5,376 KB
testcase_03 AC 24 ms
5,376 KB
testcase_04 AC 12 ms
5,376 KB
testcase_05 AC 59 ms
5,376 KB
testcase_06 AC 52 ms
5,376 KB
testcase_07 AC 31 ms
5,376 KB
testcase_08 AC 8 ms
5,376 KB
testcase_09 AC 20 ms
5,376 KB
testcase_10 AC 38 ms
5,376 KB
testcase_11 AC 36 ms
5,376 KB
testcase_12 AC 45 ms
5,376 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 51 ms
5,376 KB
testcase_16 AC 61 ms
5,376 KB
testcase_17 AC 48 ms
5,376 KB
testcase_18 AC 17 ms
5,376 KB
testcase_19 AC 51 ms
5,376 KB
testcase_20 AC 62 ms
5,376 KB
testcase_21 AC 62 ms
5,376 KB
testcase_22 AC 63 ms
5,376 KB
testcase_23 AC 63 ms
5,376 KB
testcase_24 AC 64 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,a) for(int i=0;i<(a);i++)
#define MOD 1000000007

int N;
const ll MA=1000000000;
ll a[100000];

ll solve(ll x){
  ll res=0;
  rep(i,N) res+=abs(a[i]-x);
  return res;
}

int main(){
  cin>>N;
  rep(i,N) cin>>a[i];
  sort(a,a+N);
  int lb=-MA-1,ub=MA+1;
  while(ub-lb>5){
    ll mid1=(lb*2+ub)/3;
    ll mid2=(lb+ub*2)/3;
    if(solve(mid1)>solve(mid2)) lb=mid1;
    else ub=mid2;
  }
  ll ans=LLONG_MAX/2;
  for(ll i=lb;i<=ub;i++){
    ans=min(ans,solve(i));
  }
  cout<<ans<<endl;
  return 0;
}
0