結果

問題 No.609 Noelちゃんと星々
ユーザー aim_cpoaim_cpo
提出日時 2018-02-21 18:26:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 743 bytes
コンパイル時間 2,000 ms
コンパイル使用メモリ 167,116 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 01:52:32
合計ジャッジ時間 4,159 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
4,348 KB
testcase_01 AC 23 ms
4,348 KB
testcase_02 AC 16 ms
4,348 KB
testcase_03 AC 21 ms
4,348 KB
testcase_04 AC 11 ms
4,348 KB
testcase_05 AC 53 ms
4,348 KB
testcase_06 AC 45 ms
4,348 KB
testcase_07 AC 28 ms
4,348 KB
testcase_08 AC 7 ms
4,348 KB
testcase_09 AC 18 ms
4,348 KB
testcase_10 AC 34 ms
4,348 KB
testcase_11 AC 31 ms
4,348 KB
testcase_12 AC 40 ms
4,348 KB
testcase_13 AC 8 ms
4,348 KB
testcase_14 AC 5 ms
4,348 KB
testcase_15 AC 46 ms
4,348 KB
testcase_16 AC 55 ms
4,348 KB
testcase_17 AC 44 ms
4,348 KB
testcase_18 AC 16 ms
4,348 KB
testcase_19 AC 45 ms
4,348 KB
testcase_20 AC 55 ms
4,348 KB
testcase_21 AC 55 ms
4,348 KB
testcase_22 AC 56 ms
4,348 KB
testcase_23 AC 59 ms
4,348 KB
testcase_24 AC 56 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int n;
int a[100001];

inline bool check(ll m1,ll m2){
  ll sum1=0,sum2=0;
  for(int i=0;i<n;i++){
    sum1+=abs(m1-a[i]);
    sum2+=abs(m2-a[i]);
  }
  if(sum1>=sum2)return 1;
  else return 0;
}

int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  int cont=0;
  ll l=-1000000000,r=1000000000;
  while(cont<80){
    ll mid1=(r+2*l)/3;
    ll mid2=(l+2*r)/3;
    //cerr<<mid1<<" "<<mid2<<endl;
    if(check(mid1,mid2)){
      l=mid1;
    }else{
      r=mid2;
    }
    cont++;
  }
  ll ans=(ll)1e15;
  for(int i=l;i<=r;i++){
    ll nowsum=0;
    for(int j=0;j<n;j++){
      nowsum+=abs(i-a[j]);
    }
    ans=min(ans,nowsum);
  }
  cout<<ans<<endl;
  return 0;
}
0