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;
set<int> S;
set<int> simpan[MAXN];
bool valid(int id){
if(mini==maxi)return true;
auto it=simpan[id].upper_bound(mini);
auto it2=simpan[id].lower_bound(maxi);
return it==it2;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin >> n;
for(int i=1;i<=n;i++)cin >> a[i];
S.insert(0);
S.insert(1e6+5);
simpan[0]=S;
for(int i=1;i<=n;i++){
dp[i]=1e9;
mini=a[i];maxi=a[i];
for(int j=i-1;j>=0;j--){
if(valid(j))dp[i]=min(dp[i],dp[j]+1);
mini=min(mini,a[j]);
maxi=max(maxi,a[j]);
if(a[j]>a[j+1])break;
}
S.insert(a[i]);
simpan[i]=S;
}
cout << dp[n] << '\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... |