This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e6+5;
int a[MAXN],dp[MAXN],mini,maxi,kel[MAXN],terakhir[MAXN];
set<int> S,active;
set<int>::iterator kelL,kelR;
vector<set<int>::iterator> hapus;
inline int read() {
int X=0,w=1; char c=getchar_unlocked();
while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar_unlocked(); }
while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar_unlocked();
return X*w;
}
inline bool valid(){
if(mini==maxi)return true;
kelR=S.lower_bound(maxi);
return kelL==kelR;
}
int main(){
int n;
n=read();
for(int i=1;i<=n;i++)a[i]=read();
int last=1,idx=0;
for(int i=1;i<=n;i++){
if(i==n || a[i]>a[i+1]){
idx++;
for(int j=last;j<=i;j++)kel[j]=idx;
last=i+1;
terakhir[idx]=i;
}
dp[i]=1e9;
active.insert(i);
}
S.insert(0);
S.insert(1e6+5);
for(int i=0;i<n;i++){
int ki=i+1,ka=terakhir[kel[i+1]],res=-1;
mini=a[ki];
kelL=S.upper_bound(mini);
while(ki<=ka){
int mid=(ki+ka)/2;
maxi=a[mid];
if(valid()){
res=mid;
ki=mid+1;
}
else ka=mid-1;
}
if(res!=-1){
//Ini gtw proofnya knp ga TLE, apa emang haram?
//for(int j=i+1;j<=res;j++)dp[j]=min(dp[j],dp[i]+1);
auto itL=active.lower_bound(i+1),itR=active.upper_bound(res);
for(auto it=itL;it!=itR;++it){
dp[*it]=min(dp[*it],dp[i]+1);
hapus.push_back(it);
}
for(auto isi : hapus)active.erase(isi);
hapus.clear();
}
S.insert(a[i+1]);
}
printf("%d\n",dp[n]);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |