結果

問題 No.837 Noelちゃんと星々2
ユーザー ateate
提出日時 2019-11-21 21:22:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,155 bytes
コンパイル時間 2,390 ms
コンパイル使用メモリ 215,544 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-10-11 01:21:26
合計ジャッジ時間 4,565 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
using pll = pair<ll,ll>;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
//constexpr ll MOD = (998244353);
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
void dump(){cout<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cout<<h<<", ";dump(forward<Ts>(t)...);}
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}

void solve1();void solve2();
int main(){
  solve2();
  return 0;
}

void solve2(){

  ll n;
  cin>>n;
  vector<ll> a(n);
  cin>>a;

  map<ll,ll> cnt;
  for(auto x:a)cnt[x]++;
  if(cnt.size()==1){cout<<1<<endl;return;}
  if(cnt.size()==2){cout<<0<<endl;return;}

  sort(a.begin(),a.end());
  vector<ll> sum(n+1);
  rep(i,n)sum[i+1]=sum[i]+a[i];
  ll ans = INF;
  for(ll mid=1;mid<n-1;++mid){
    ll lmid = mid/2;
    ll rmid = (n-mid)/2;
    ll sumll = sum[lmid+1]-sum[0];
    ll sumlr = sum[mid+1]-sum[lmid];
    ll sumrl = sum[rmid+1]-sum[mid];
    ll sumrr = sum[n]-sum[rmid];
    ll distll = a[lmid]*(n/4)-sumll;
    ll distlr = sumlr-a[lmid]*(n/4);
    ll distrl = a[rmid]*(n/4)-sumrl;
    ll distrr = sumrr-a[rmid]*(n/4);
    chmin(ans,min(distll+distlr,distrl+distrr));
  }
  cout<<(ans)<<endl;

}

void solve1(){

  ll n;
  cin>>n;
  vector<ll> a(n);
  cin>>a;

  sort(a.begin(),a.end());
  ll base1 = a[n/3];
  ll base2 = a[n/3*2];
  ll ans = 0;
  rep(i,n){
    ans += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  base1 = a[n/3+1];
  base2 = a[n/3*2+1];
  ll tmp = 0;
  rep(i,n){
    tmp += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  chmin(ans,tmp);
  tmp=0;
  base1 = a[n/3-1];
  base2 = a[n/3*2-1];
  rep(i,n){
    tmp += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  chmin(ans,tmp);
  tmp=0;
  base1 = a[n/3+1];
  base2 = a[n/3*2-1];
  rep(i,n){
    tmp += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  chmin(ans,tmp);
  tmp=0;
  base1 = a[n/3-1];
  base2 = a[n/3*2+1];
  rep(i,n){
    tmp += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  chmin(ans,tmp);
  tmp=0;
  base1 = a[n/3];
  base2 = a[n/3*2-1];
  rep(i,n){
    tmp += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  chmin(ans,tmp);
  tmp=0;
  base1 = a[n/3-1];
  base2 = a[n/3*2];
  rep(i,n){
    tmp += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  chmin(ans,tmp);
  tmp=0;
  base1 = a[n/3+1];
  base2 = a[n/3*2];
  rep(i,n){
    tmp += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  chmin(ans,tmp);
  tmp=0;
  base1 = a[n/3];
  base2 = a[n/3*2+1];
  rep(i,n){
    tmp += min(abs(base1-a[i]),abs(base2-a[i]));
  }
  chmin(ans,tmp);

  map<ll,ll>cnt;
  rep(i,n)cnt[a[i]]++;
  if(cnt.size()==1)cout<<1<<endl;
  else if(cnt.size()==2)cout<<0<<endl;
  else cout<<ans<<endl;

}
0