Submission #167411

# Submission time Handle Problem Language Result Execution time Memory
167411 2019-12-08T09:35:05 Z theStaticMind Kralj (COCI16_kralj) C++14
56 / 140
2000 ms 79864 KB
#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 time Memory Grader output
1 Correct 853 ms 62328 KB Output is correct
2 Correct 858 ms 60408 KB Output is correct
3 Correct 724 ms 75168 KB Output is correct
4 Correct 702 ms 76524 KB Output is correct
5 Execution timed out 2057 ms 72452 KB Time limit exceeded
6 Execution timed out 2065 ms 71576 KB Time limit exceeded
7 Execution timed out 2021 ms 75512 KB Time limit exceeded
8 Execution timed out 2056 ms 67576 KB Time limit exceeded
9 Execution timed out 2056 ms 79864 KB Time limit exceeded
10 Execution timed out 2073 ms 78844 KB Time limit exceeded