Submission #167411

#TimeUsernameProblemLanguageResultExecution timeMemory
167411theStaticMindKralj (COCI16_kralj)C++14
56 / 140
2073 ms79864 KiB
#include<bits/stdc++.h> #define mp make_pair #define pb push_back #define ii pair<int,int> #define all(x) (x).begin(),(x).end() #define INF 100000000000000000 #define modulo 1000000007 #define mod 998244353 #define int long long int using namespace std; int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); // freopen("q.gir","r",stdin); // freopen("q.cik","w",stdout); int n,ans=0; cin>>n; vector<int>A(n); vector<int>P(n); vector<int>V(n); vector<int>cnt(n,0); vector<int>ptr(n); for(int i=0;i<n;i++){ cin>>A[i];A[i]--; cnt[A[i]]++; ptr[i]=i; } for(int i=0;i<n;i++){ if(cnt[i]==0)continue; int c=cnt[i]; int ind=i; while(c>0){ c--; if(c==0)break; ind++; while(ptr[ind%n]!=ind%n)ind=ptr[ind%n]; c+=cnt[(ind)%n]; } ptr[i]=(ind+1)%n; if(i<ptr[i]) i=ptr[i]; else break; } int mx=0,ind=-1; for(int i=0;i<n;i++){ if(cnt[i]==0)continue; if(ptr[i]<=i){ if(mx<ptr[i]+n-i){ mx=ptr[i]+n-i; ind=i; } } else if(mx<ptr[i]-i){ mx=ptr[i]-i; ind=i; } } for(int i=0;i<n;i++)cin>>P[i]; for(int i=0;i<n;i++)cin>>V[i]; multiset<int> data[n]; for(int i=0;i<n;i++){ data[A[i]].insert(V[i]); } for(int k=0;k<n;k++,ind=(ind+1)%n){ multiset<int>::iterator itr=data[ind].upper_bound(P[ind]); if(itr==data[ind].end()){ data[ind].erase(data[ind].begin()); } else{ data[ind].erase(itr); ans++; } int y=(ind+1)%n; if(data[ind].size()>data[y].size())swap(data[ind],data[y]); for(itr=data[ind].begin();itr!=data[ind].end();itr++){ data[y].insert(*itr); } } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...