結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー LayCurseLayCurse
提出日時 2020-06-26 21:32:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 6,186 bytes
コンパイル時間 2,761 ms
コンパイル使用メモリ 218,676 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-07-04 19:48:28
合計ジャッジ時間 4,798 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 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 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 6 ms
5,376 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 6 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 6 ms
5,376 KB
testcase_22 AC 5 ms
5,376 KB
testcase_23 AC 95 ms
12,032 KB
testcase_24 AC 96 ms
12,032 KB
testcase_25 AC 96 ms
12,160 KB
testcase_26 AC 96 ms
12,032 KB
testcase_27 AC 96 ms
12,032 KB
testcase_28 AC 64 ms
11,904 KB
testcase_29 AC 62 ms
11,904 KB
testcase_30 AC 75 ms
12,032 KB
testcase_31 AC 77 ms
12,032 KB
testcase_32 AC 76 ms
12,032 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:287:12: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
  287 |       chmin(res, t1 + A[i] + t2);
      |       ~~~~~^~~~~~~~~~~~~~~~~~~~~
main.cpp:191:7: note: 'res' was declared here
  191 |     T res;
      |       ^~~
main.cpp:192:7: warning: 'tmp' may be used uninitialized [-Wmaybe-uninitialized]
  192 |     T tmp;
      |       ^~~
main.cpp:287:21: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
  287 |       chmin(res, t1 + A[i] + t2);
      |                  ~~~^~~~~~
main.cpp:191:7: note: 'res' was declared here
  191 |     T res;
      |       ^~~
main.cpp:192:7: warning: 'tmp' may be used uninitialized [-Wmaybe-uninitialized]
  192 |     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 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);
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
  sort(a, a+N);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
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');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
template<class T> struct segtree_Point_Minval{
  int N;
  int logN;
  T *mn;
  void malloc(int maxN, int once = 0){
    int i;
    for(i=1;i<maxN;i*=2){
      ;
    }
    mn = new T[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);
    if(once){
      setN(maxN);
    }
  }
  void free(void){
    delete [] mn;
  }
  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=N-1;i;i--){
      mn[i] =min_L(mn[2*i], mn[2*i+1]);
    }
  }
  inline void build(int a){
    while(a > 1){
      a /= 2;
      mn[a] =min_L(mn[2*a], mn[2*a+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 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;
  }
}
;
int N;
int A[200000];
int mn1[200000+1];
int mn2[200000+1];
int v[200000];
int ind[200000];
int main(){
  int V9aVTaxx, i;
  wmem = memarr;
  int res = 1073709056;
  int t1;
  int t2;
  segtree_Point_Minval<int> t;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  mn1[0] = mn2[0] = 1073709056;
  for(i=(0);i<(N);i++){
    mn1[i+1] =min_L(mn1[i], A[i]);
    mn2[i+1] =min_L(mn2[i], A[N-1-i]);
  }
  for(i=(1);i<(N-1);i++){
    if(mn1[i] < A[i] && A[i] > mn2[N-i-1]){
      chmin(res, mn1[i] + A[i] + mn2[N-i-1]);
    }
  }
  for(i=(0);i<(N);i++){
    {
      auto ao_dF3pO = (A[i]);
      auto tU__gIr_ = ( i);
      v[i] = ao_dF3pO;
      ind[i] = tU__gIr_;
    }
  }
  sortA_L(N, v, ind);
  t.malloc(N,1);
  for(i=(0);i<(N);i++){
    t.change(i, 1073709056);
  }
  for(V9aVTaxx=(N)-1;V9aVTaxx>=(0);V9aVTaxx--){
    auto &i = ind[V9aVTaxx];
    t.change(i, A[i]);
    if(i == 0 || i == N-1){
      continue;
    }
    t1 = t.getMinVal(0,i);
    t2 = t.getMinVal(i+1,N);
    if(t1 != 1073709056 && t2 != 1073709056){
      chmin(res, t1 + A[i] + t2);
    }
  }
  if(res==1073709056){
    wt_L(-1);
    wt_L('\n');
  }
  else{
    wt_L(res);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N, A[2d5];
// int mn1[2d5+1], mn2[2d5+1];
// int v[2d5], ind[2d5];
// {
//   int res = int_inf, t1, t2;
//   segtree_Point_Minval<int> t;
//   rd(N,A(N));
// 
//   mn1[0] = mn2[0] = int_inf;
//   rep(i,N){
//     mn1[i+1] = min(mn1[i], A[i]);
//     mn2[i+1] = min(mn2[i], A[N-1-i]);
//   }
//   rep(i,1,N-1) if(mn1[i] < A[i] && A[i] > mn2[N-i-1]) res <?= mn1[i] + A[i] + mn2[N-i-1];
// 
//   rep(i,N) (v[i], ind[i]) = (A[i], i);
//   sortA(N, v, ind);
//   t.malloc(N,1);
//   rep(i,N) t.change(i, int_inf);
// 
//   rrep[ind](i,N){
//     t.change(i, A[i]);
//     if(i == 0 || i == N-1) continue;
//     t1 = t.getMinVal(0,i);
//     t2 = t.getMinVal(i+1,N);
//     if(t1 != int_inf && t2 != int_inf) res <?= t1 + A[i] + t2;
//   }
//   wt(if[res==int_inf, -1, res]);
// }
0