結果

問題 No.1117 数列分割
ユーザー LayCurseLayCurse
提出日時 2020-07-17 21:43:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,752 ms / 3,000 ms
コード長 7,239 bytes
コンパイル時間 2,990 ms
コンパイル使用メモリ 215,204 KB
実行使用メモリ 7,752 KB
最終ジャッジ日時 2024-05-07 11:18:35
合計ジャッジ時間 20,012 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 69 ms
6,944 KB
testcase_04 AC 122 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 104 ms
6,940 KB
testcase_09 AC 29 ms
6,944 KB
testcase_10 AC 106 ms
6,940 KB
testcase_11 AC 458 ms
6,944 KB
testcase_12 AC 490 ms
7,752 KB
testcase_13 AC 696 ms
6,944 KB
testcase_14 AC 749 ms
6,940 KB
testcase_15 AC 793 ms
6,940 KB
testcase_16 AC 1,053 ms
6,940 KB
testcase_17 AC 110 ms
6,940 KB
testcase_18 AC 821 ms
6,940 KB
testcase_19 AC 1,033 ms
6,944 KB
testcase_20 AC 930 ms
6,940 KB
testcase_21 AC 1,726 ms
6,940 KB
testcase_22 AC 1,720 ms
6,940 KB
testcase_23 AC 1,732 ms
6,944 KB
testcase_24 AC 1,682 ms
6,940 KB
testcase_25 AC 1,752 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 46 ms
6,940 KB
testcase_28 AC 46 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:356:14: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
  356 |         chmax(nx[i], -t.getMinVal(max_L(0, i-M), i) + s);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:267:7: note: 'res' was declared here
  267 |     T res;
      |       ^~~
main.cpp:268:7: warning: 'tmp' may be used uninitialized [-Wmaybe-uninitialized]
  268 |     T tmp;
      |       ^~~
main.cpp:346:14: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
  346 |         chmax(nx[i], -t.getMinVal(max_L(0, i-M), i) + s);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:267:7: note: 'res' was declared here
  267 |     T res;
      |       ^~~
main.cpp:268:7: warning: 'tmp' may be used uninitialized [-Wmaybe-uninitialized]
  268 |     T tmp;
      |       ^~~

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
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;
  }
}
inline void rd(long long &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(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;
}
template<class T> struct segtree_Point_Min{
  int N;
  int logN;
  T *mn;
  int *mnind;
  void malloc(int maxN, int once = 0){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    mn = new T[2*i];
    mnind = new int[2*i];
    if(once){
      setN(maxN);
    }
  }
  void walloc(int maxN, int once = 0, void **mem = &wmem){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    walloc1d(&mn, 2*i, mem);
    walloc1d(&mnind, 2*i, mem);
    if(once){
      setN(maxN);
    }
  }
  void free(void){
    delete [] mn;
    delete [] mnind;
  }
  T& operator[](int i){
    return mn[N+i];
  }
  void setN(int n, int zerofill = 1, int dobuild = 1){
    int i;
    for(i=1,logN=0;i<n;i*=2,logN++){
      ;
    }
    N = i;
    if(zerofill){
      for(i=(0);i<(N);i++){
        mn[N+i] = 0;
      }
    }
    if(dobuild){
      build();
    }
  }
  void build(void){
    int i;
    for(i=(0);i<(N);i++){
      mnind[N+i] = i;
    }
    for(i=N-1;i;i--){
      if(mn[2*i] <= mn[2*i+1]){
        mn[i] = mn[2*i];
        mnind[i] = mnind[2*i];
      }
      else{
        mn[i] = mn[2*i+1];
        mnind[i] = mnind[2*i+1];
      }
    }
  }
  inline void build(int a){
    while(a > 1){
      a /= 2;
      if(mn[a*2] <= mn[a*2+1]){
        mn[a] = mn[a*2];
        mnind[a] = mnind[a*2];
      }
      else{
        mn[a] = mn[a*2+1];
        mnind[a] = mnind[a*2+1];
      }
    }
  }
  inline void change(int a, T val){
    mn[a+N] = val;
    build(a+N);
  }
  inline void add(int a, T val){
    mn[a+N] += val;
    build(a+N);
  }
  inline pair<T,int> getMin(int a, int b){
    pair<T,int> res;
    pair<T,int> tmp;
    int fga = 0;
    int fgb = 0;
    a += N;
    b += N;
    while(a < b){
      if(a%2){
        if(fga){
          res =min_L(res, make_pair(mn[a], mnind[a]));
        }
        else{
          res = make_pair(mn[a], mnind[a]);
          fga = 1;
        }
        a++;
      }
      if(b%2){
        b--;
        if(fgb){
          tmp =min_L(make_pair(mn[b], mnind[b]), tmp);
        }
        else{
          tmp = make_pair(mn[b], mnind[b]);
          fgb = 1;
        }
      }
      a /= 2;
      b /= 2;
    }
    if(fga==1 && fgb==0){
      return res;
    }
    if(fga==0 && fgb==1){
      return tmp;
    }
    if(fga==1 && fgb==1){
      res =min_L(res, tmp);
      return res;
    }
    return res;
  }
  inline T getMinVal(int a, int b){
    T res;
    T tmp;
    int fga = 0;
    int fgb = 0;
    a += N;
    b += N;
    while(a < b){
      if(a%2){
        if(fga){
          res =min_L(res, mn[a]);
        }
        else{
          res = mn[a];
          fga = 1;
        }
        a++;
      }
      if(b%2){
        b--;
        if(fgb){
          tmp =min_L(mn[b], tmp);
        }
        else{
          tmp = mn[b];
          fgb = 1;
        }
      }
      a /= 2;
      b /= 2;
    }
    if(fga==1 && fgb==0){
      return res;
    }
    if(fga==0 && fgb==1){
      return tmp;
    }
    if(fga==1 && fgb==1){
      res =min_L(res, tmp);
      return res;
    }
    return res;
  }
  inline int getMinInd(int a, int b){
    return getMin(a,b).second;
  }
}
;
int N;
int K;
int M;
long long A[3000];
long long dp[3001];
long long nx[3001];
int main(){
  int RZTsC2BF, i;
  wmem = memarr;
  segtree_Point_Min<long long> t;
  long long s;
  rd(N);
  rd(K);
  rd(M);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(N+1);i++){
    dp[i] = -4611686016279904256LL;
  }
  dp[0] = 0;
  t.malloc(N+1, 1);
  for(RZTsC2BF=(0);RZTsC2BF<(K);RZTsC2BF++){
    for(i=(0);i<(N+1);i++){
      nx[i] = -4611686016279904256LL;
    }
    s = 0;
    for(i=(0);i<(N+1);i++){
      if(i){
        chmax(nx[i], -t.getMinVal(max_L(0, i-M), i) + s);
      }
      if(i<N){
        t.change(i, -(dp[i] - s));
        s += A[i];
      }
    }
    s = 0;
    for(i=(0);i<(N+1);i++){
      if(i){
        chmax(nx[i], -t.getMinVal(max_L(0, i-M), i) + s);
      }
      if(i<N){
        t.change(i, -(dp[i] - s));
        s -= A[i];
      }
    }
    for(i=(0);i<(N+1);i++){
      dp[i] = nx[i];
    }
  }
  wt_L(dp[N]);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N, K, M;
// ll A[3000];
// ll dp[3001], nx[3001];
// {
//   segtree_Point_Min<ll> t;
//   ll s;
//   rd(N,K,M,A(N));
// 
//   rep(i,N+1) dp[i] = -ll_inf;
//   dp[0] = 0;
// 
//   t.malloc(N+1, 1);
// 
//   rep(K){
//     rep(i,N+1) nx[i] = -ll_inf;
//     s = 0;
//     rep(i,N+1){
//       if(i){
//         nx[i] >?= -t.getMinVal(max(0,i-M), i) + s;
//       }
//       if(i<N){
//         t.change(i, -(dp[i] - s));
//         s += A[i];
//       }
//     }
//     s = 0;
//     rep(i,N+1){
//       if(i){
//         nx[i] >?= -t.getMinVal(max(0,i-M), i) + s;
//       }
//       if(i<N){
//         t.change(i, -(dp[i] - s));
//         s -= A[i];
//       }
//     }
//     rep(i,N+1) dp[i] = nx[i];
//   }
// 
//   wt(dp[N]);
// }
0