結果

問題 No.838 Noelちゃんと星々3
ユーザー LayCurseLayCurse
提出日時 2019-07-06 12:12:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,793 bytes
コンパイル時間 2,352 ms
コンパイル使用メモリ 204,752 KB
実行使用メモリ 5,180 KB
最終ジャッジ日時 2023-10-25 08:41:14
合計ジャッジ時間 4,061 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
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');
  }
}
int N;
long long Y[100000];
long long dp[100001];
int main(){
  int i, j, k;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=0;Lj4PdHRW<N;Lj4PdHRW++){
      rd(Y[Lj4PdHRW]);
    }
  }
  sort(Y, Y+N);
  dp[0] = 0;
  dp[1] = 4611686016279904256LL;
  dp[2] = Y[1]-Y[0];
  for(i=2;i<N;i++){
    dp[i+1] =min_L(dp[i-1]+Y[i]-Y[i-1], dp[i-2]+Y[i]-Y[i-2]);
  }
  wt_L(dp[N]);
  wt_L('\n');
  return 0;
}
// cLay varsion 20190706-1 [beta]

// --- original code ---
// int N; ll Y[1d5];
// ll dp[100001];
// {
//   int i, j, k;
//   rd(N,Y(N));
// 
//   sort(Y, Y+N);
// 
//   dp[0] = 0;
//   dp[1] = ll_inf;
//   dp[2] = Y[1]-Y[0];
//   rep(i,2,N) dp[i+1] = min(dp[i-1]+Y[i]-Y[i-1], dp[i-2]+Y[i]-Y[i-2]);
// 
//   wt(dp[N]);
// }
0