結果

問題 No.1007 コイン集め
ユーザー LayCurseLayCurse
提出日時 2020-03-06 21:37:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 1,500 ms
コード長 4,271 bytes
コンパイル時間 2,497 ms
コンパイル使用メモリ 208,320 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 05:24:02
合計ジャッジ時間 3,348 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#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 int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
int N;
int K;
int A[200000];
int t[200000];
long long solve(int a, int b){
  int i;
  int x;
  long long res = 0;
  for(i=(a);i<(b+1);i++){
    res += A[i];
  }
  return res;
  if(a==b && A[a]){
    return 1;
  }
  res += 2;
  t[a] = A[a] - 1;
  for(i=(a+1);i<(b);i++){
    t[i] = A[i] - 2;
    res += 2;
  }
  t[b] = A[b] - 1;
  for(i=(a+1);i<(b+1);i++){
    x =min_L(t[i-1], t[i]);
    res += 2*x;
    {
      auto xr20shxY = (x);
      t[i-1] -= xr20shxY;
      t[i] -= xr20shxY;
    }
  }
  wt_L(a);
  wt_L(' ');
  wt_L(b);
  wt_L(' ');
  wt_L(res);
  wt_L('\n');
  return res;
}
int main(){
  int a;
  int b;
  long long res = 0;
  rd(N);
  rd(K);K += (-1);
  {
    int WYIGIcGE;
    for(WYIGIcGE=(0);WYIGIcGE<(N);WYIGIcGE++){
      rd(A[WYIGIcGE]);
    }
  }
  if(A[K] >= 1){
    a = K-1;
    while(a >= 0 && A[a] >= 2){
      a--;
    }
    if(a < 0 || A[a]==0){
      a++;
    }
    chmax(res, solve(a,K));
    b = K+1;
    while(b < N && A[b] >= 2){
      b++;
    }
    if(b >= N || A[b]==0){
      b--;
    }
    chmax(res, solve(K,b));
  }
  if(A[K] >= 2){
    a = b = K;
    while(a >= 0 && A[a] >= 2){
      a--;
    }
    if(a < 0 || A[a]==0){
      a++;
    }
    while(b < N && A[b] >= 2){
      b++;
    }
    if(b >= N || A[b]==0){
      b--;
    }
    chmax(res, solve(a,b));
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200303-1 [beta]

// --- original code ---
// int N, K, A[2d5];
// 
// int t[2d5];
// ll solve(int a, int b){
//   int x;
//   ll res = 0;
// 
//   rep(i,a,b+1) res += A[i];
//   return res;
//   
//   if(a==b && A[a]) return 1;
// 
//   res += 2;
//   t[a] = A[a] - 1;
//   rep(i,a+1,b) t[i] = A[i] - 2, res += 2;
//   t[b] = A[b] - 1;
// 
//   rep(i,a+1,b+1){
//     x = min(t[i-1], t[i]);
//     res += 2*x;
//     (t[i-1], t[i]) -= x;
//   }
// 
//   wt(a,b,res);
//   return res;
// }
// 
// {
//   int a, b;
//   ll res = 0;
//   rd(N,K--,A(N));
// 
//   if(A[K] >= 1){
//     a = K-1;
//     while(a >= 0 && A[a] >= 2) a--;
//     if(a < 0 || A[a]==0) a++;
//     res >?= solve(a,K);
// 
//     b = K+1;
//     while(b < N && A[b] >= 2) b++;
//     if(b >= N || A[b]==0) b--;
//     res >?= solve(K,b);
//   }
// 
//   if(A[K] >= 2){
//     a = b = K;
//     while(a >= 0 && A[a] >= 2) a--;
//     if(a < 0 || A[a]==0) a++;
//     while(b < N && A[b] >= 2) b++;
//     if(b >= N || A[b]==0) b--;
//     res >?= solve(a,b);
//   }
// 
//   wt(res);
// }
0