結果

問題 No.837 Noelちゃんと星々2
ユーザー LayCurseLayCurse
提出日時 2019-06-14 21:38:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 2,743 bytes
コンパイル時間 2,428 ms
コンパイル使用メモリ 202,376 KB
実行使用メモリ 5,916 KB
最終ジャッジ日時 2023-09-02 07:18:13
合計ジャッジ時間 3,727 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
inline void rd(int &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(long long x){
  char f[20];
  int m=0, s=0;
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
int N;
long long Y[100000];
long long res1[100001];
long long res2[100001];
void solve(long long res[]){
  int i;
  long long cnt1=0, cnt2=0, mid, sum1=0, sum2=0;
  for(i=0;i<N;i++){
    if(i%2==0){
      cnt2++;
      sum2+=Y[i];
    }
    else{
      cnt1++;
      sum1+=Y[i/2];
      sum2+=Y[i]-Y[i/2];
    }
    mid = Y[i/2];
    res[i+1] = (mid*cnt1 - sum1) + (sum2 - mid*cnt2);
  }
}
int main(){
  int i;
  long long res;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=0;Lj4PdHRW<N;Lj4PdHRW++){
      rd(Y[Lj4PdHRW]);
    }
  }
  sort(Y, Y+N);
  if(Y[0]==Y[N-1]){
    res = 1;
  }
  else{
    solve(res1);
    reverse(Y,Y+N);
    for(i=0;i<N;i++){
      Y[i] *= -1;
    }
    solve(res2);
    res = 4611686016279904256LL;
    for(i=1;i<N;i++){
      chmin(res, res1[i] + res2[N-i]);
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20190609-2 [beta]

// --- original code ---
// int N;
// ll Y[1d5];
// ll res1[100001], res2[100001];
// 
// void solve(ll res[]){
//   int i;
//   ll mid;
//   ll cnt1 = 0, cnt2 = 0;
//   ll sum1 = 0, sum2 = 0;
// 
//   rep(i,N){
//     if(i%2==0){
//       cnt2++;
//       sum2+=Y[i];
//     } else {
//       cnt1++;
//       sum1+=Y[i/2];
//       sum2+=Y[i]-Y[i/2];
//     }
//     mid = Y[i/2];
// 
//     res[i+1] = (mid*cnt1 - sum1) + (sum2 - mid*cnt2);
//   }
// }
// 
// {
//   int i;
//   ll res;
//   rd(N,Y(N));
//   sort(Y, Y+N);
// 
//   if(Y[0]==Y[N-1]){
//     res = 1;
//   } else {
//     solve(res1);
//     reverse(Y,Y+N);
//     rep(i,N) Y[i] *= -1;
//     solve(res2);
// 
//     res = ll_inf;
//     rep(i,1,N) res <?= res1[i] + res2[N-i];
//   }
// 
//   wt(res);
// }
0