Submission #341589

#TimeUsernameProblemLanguageResultExecution timeMemory
341589nandonathanielMoney (IZhO17_money)C++14
45 / 100
1552 ms61036 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,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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...