이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
const int LG = 20;
struct node {
  long long mn=0;
  vector<int> dec;
  node() {
    dec=vector<int>(LG, -1);
  }
};
int n, a[N];
node up[N][LG];
node merge(const node &A, const node &B) {
  node res;
  res.mn = min(A.mn, B.mn);
  for(int i = 0;i<LG;i++){
    if(A.dec[i] == -1 or B.dec[i] == -1)continue;
    res.dec[i] = A.dec[i]+B.dec[i];
  }
  if(res.mn > 0) {
    for(int i = 0;i+1<LG;i++){
      if(A.dec[i+1] == -1 or B.dec[i+1]==-1)continue;
      int L = A.dec[i+1];
      int R = B.dec[i+1];
      if(L == -1 or R == -1)continue;
      if(res.dec[i] == -1)res.dec[i] = L+R+1;
      else res.dec[i] = min(res.dec[i], L+R+1);
    }
  }
  return res;
}
int main () {
  cin >> n;
  for(int i = 0;i<n;i++) {
    cin >> a[i];
    node tmp;
    tmp.mn = a[i];
    if(a[i] >= LG) {
      cout << "-1\n";
      return 0;
    }
    tmp.dec[a[i]] = 0;
    if(a[i] >0)tmp.dec[a[i]-1]=1;
    up[i][0] = tmp;
  }
  for(int i = 0;i<n;i++) {
    for(int j = 1;j<LG;j++) {
      int prv = i-(1<<(j-1));
      if(prv<0)break;
      up[i][j] = merge(up[i][j-1], up[prv][j-1]);
    }
  }
  long long dp[n+1];
  dp[0]=0;
  for(int i = 1;i<=n;i++) {
    int cnt=0;
    dp[i] = 1e18;
    for(int j = 1;j<=i;j*=2) {
      if(up[i-1][cnt].dec[0]!=-1)
        dp[i] = min(dp[i], dp[i-j]+up[i-1][cnt].dec[0]);
      cnt++;
    }
  }
  if(dp[n] == 1e18) {
    cout << "-1\n";
    return 0;
  }
  else cout << dp[n] << "\n";
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |