結果

問題 No.837 Noelちゃんと星々2
ユーザー beetbeet
提出日時 2019-06-14 21:45:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,181 bytes
コンパイル時間 1,964 ms
コンパイル使用メモリ 204,000 KB
実行使用メモリ 5,572 KB
最終ジャッジ日時 2023-09-02 07:19:31
合計ジャッジ時間 3,516 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<Int> ys(n);
  for(Int i=0;i<n;i++) cin>>ys[i];
  sort(ys.begin(),ys.end());

  if(ys.front()==ys.back()){
    cout<<1<<endl;
    return 0;
  }
  // dp1: [l, r)
  // dp2: [r, n)
  vector<Int> dp1(n+1,0),dp2(n,0);
  {
    Int sum=0,res=0;
    for(Int i=0;i<n;i++){
      sum+=ys[i];
      Int j=i/2;
      if(~i&1) res+=ys[j];
      dp1[i+1]=(ys[j]*(j+1)-res)+((sum-res)-ys[j]*(i-j));
      //cout<<i<<" "<<j<<":"<<sum<<" "<<res<<":"<<dp1[i+1]<<endl;
    }
  }
  {
    Int sum=0,res=0;
    for(Int i=n-1;i>=0;i--){
      sum+=ys[i];
      Int j=i+(n-i)/2;
      if((n-i)&1) res+=ys[j];
      dp2[i]=(ys[j]*(j-i)-(sum-res))+(res-ys[j]*(n-j));
      //cout<<i<<" "<<j<<":"<<dp2[i]<<endl;
    }
  }
  Int ans=dp1[0]+dp2[0];
  for(Int i=0;i<n;i++) chmin(ans,dp1[i]+dp2[i]);
  cout<<ans<<endl;
  return 0;
}
0