Submission #533944

#TimeUsernameProblemLanguageResultExecution timeMemory
533944michaoFinancial Report (JOI21_financial)C++14
100 / 100
440 ms34000 KiB
#include <bits/stdc++.h> #define int long long #define mp make_pair #define pb push_back #define ld long double #define pii pair<int,int> #define sz(x) (int)x.size() #define piii pair<pii,pii> #define precise cout<<fixed<<setprecision(10) #define st first #define nd second #define ins insert #define vi vector<int> #define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; const int MAX=3e5+5; const int inf=(int)1e18+9; int t[MAX*2]; int a[MAX]; int p[MAX]; int minid[MAX]; void update(int u,int c){ for (t[u+=MAX]=c;u>1;u>>=1)t[u>>1]=max(t[u],t[u^1]); } int query(int u,int v){ int maxi=0; for (u+=MAX,v+=MAX+1;u<v;u>>=1,v>>=1){ if (u&1)maxi=max(maxi,t[u++]); if (v&1)maxi=max(maxi,t[--v]); } return maxi; } bool cmp(pii a,pii b){ if (a.st!=b.st)return a.st<b.st; return a.nd>b.nd; } set<int>checked; int Find(int u){ if (p[u]!=u)p[u]=Find(p[u]); return p[u]; } void Union(int u,int v){ minid[Find(v)]=min(minid[Find(v)],minid[Find(u)]); p[Find(u)]=Find(v); } int32_t main(){ BOOST; int n,d; cin>>n>>d; vector<pii>pom; for (int i=1;i<=n;i++){ cin>>a[i]; pom.pb(mp(a[i],i)); } for (int i=1;i<=n;i++)p[i]=i,minid[i]=i; checked.ins(-1); checked.ins(n+1); int ans=0; sort(pom.begin(),pom.end(),cmp); for (auto iter:pom){ int id=iter.nd; auto it=*(--checked.lower_bound(id)); if (it!=-1 && id-it<=d)Union(id,it); it=*checked.upper_bound(id); if (it!=n+1 && it-id<=d)Union(id,it); int l=minid[Find(id)]; int r=id; int dpek=query(l,r)+1; update(id,dpek); ans=max(ans,dpek); checked.ins(id); } cout<<ans; 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...