Submission #599113

#TimeUsernameProblemLanguageResultExecution timeMemory
599113MohammadAghilThe short shank; Redemption (BOI21_prison)C++17
100 / 100
411 ms324904 KiB
      #include <bits/stdc++.h>
//   #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
    using namespace std;
  typedef long long ll;
   typedef pair<int, int> pp;
    #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
      #define per(i,r,l) for(int i = (r); i >= (l); i--)
        #define rep(i,l,r) for(int i = (l); i < (r); i++)
           #define all(x) begin(x), end(x)
              #define sz(x) (int)(x).size()
                  #define pb push_back
                      #define ss second
                           #define ff first
                                   void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
     cin.tie(0) -> sync_with_stdio(0);
     // #ifndef ONLINE_JUDGE
     //      freopen("in.in", "r", stdin);
     //      freopen("out.out", "w", stdout);
     // #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 2e6 + 5, lg = 22, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}

vector<int> adj[maxn];
int par[maxn], h[maxn];
pp ptr[maxn];
bool vis[maxn];

void dfs(int r){
     vis[r] = true, ptr[r] = {1, r};
     for(int c: adj[r]) h[c] = h[r] + 1, dfs(c), ptr[r] = max(ptr[r], {ptr[c].ff + 1, ptr[c].ss});
}

int main(){ IOS();
     int n, d, t; cin >> n >> d >> t;
     vector<int> a(n), e(n);
     rep(i,0,n) cin >> a[i], e[i] = t - a[i] + i;

     vector<bool> is(n);

     int mx = -1, ans = 0;
     rep(i,0,n){
          if(a[i] > t) {
               if(mx < i) ans++;
               else is[i] = true;
          }
          mx = max(mx, e[i]);
     }

     vector<int> stk;
     per(i,n-1,0){
          if(is[i]){
               par[i] = -1;
               if(sz(stk)) par[i] = stk.back(), adj[par[i]].pb(i);
               stk.pb(i);
          }else{
               while(sz(stk) && stk.back() <= e[i]) stk.pop_back();
          }
     }

     vector<vector<int>> q(n + 1);

     per(i,n-1,0) if(is[i]){
          if(!vis[i]) h[i] = 1, dfs(i), q[ptr[i].ff].pb(i);
     }


     per(i,n,1){
          for(int c: q[i]){
               int lst = -1, r = ptr[c].ss;
               while(r - par[c]){
                    ans++;
                    for(int t: adj[r]) if(t - lst){
                         q[ptr[t].ff].pb(t);
                    }
                    lst = r, r = par[r];
               }
               d--;
               if(!d) break;
          }
          if(!d) break; 
     }

     cout << n - ans << endl;
     return 0; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...