結果
問題 | No.837 Noelちゃんと星々2 |
ユーザー | ate |
提出日時 | 2019-11-22 13:44:32 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 119 ms / 2,000 ms |
コード長 | 3,827 bytes |
コンパイル時間 | 2,391 ms |
コンパイル使用メモリ | 209,440 KB |
最終ジャッジ日時 | 2025-01-08 04:28:00 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 29 |
ソースコード
#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(); void solve3(); int main(){ solve3(); return 0; } void solve3(){ 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 res = INF; for(ll i=1;i<n-1;++i){ ll j = n-i; ll ans = sum[i]-sum[i/2]*2; if(i&1)ans-=a[i/2]; ans += sum[n]-sum[i]-(sum[i+j/2]-sum[i])*2; if(j&1)ans-=a[i+j/2]; chmin(res,ans); } cout<<(res)<<endl; } 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;++mid){ ll lmid = mid/2; ll rmid = mid+(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]*(lmid+1)-sumll; ll distlr = sumlr-a[lmid]*(mid-lmid+1); ll distrl = a[rmid]*(rmid-mid+1)-sumrl; ll distrr = sumrr-a[rmid]*(n-rmid); dump(); dump(lmid,mid,rmid,a[lmid],a[rmid]); dump(sumll,sumlr,sumrl,sumrr); dump(distll,distlr,distrl,distrr); chmin(ans,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; }