Submission #341562

#TimeUsernameProblemLanguageResultExecution timeMemory
341562nandonathanielMoney (IZhO17_money)C++14
100 / 100
1473 ms67236 KiB
#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; bool valid(){ if(mini==maxi)return true; auto it=S.upper_bound(mini); auto it2=S.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]; 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; } 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]; 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){ for(int j=i+1;j<=res;j++)dp[j]=min(dp[j],dp[i]+1); } S.insert(a[i+1]); } cout << dp[n] << '\n'; 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...