Submission #589247

#TimeUsernameProblemLanguageResultExecution timeMemory
589247BT21tataGondola (IOI14_gondola)C++17
100 / 100
86 ms11000 KiB
#include "gondola.h" #include <bits/stdc++.h> typedef long long ll; using namespace std; const ll MOD=1e9+9; map<int,int> hp; int valid(int n, int a[]) { int val=-1; for(int i=0; i<n; i++) { if(a[i]<=n) { if(val==-1) { val=a[i]+1; if(val==n+1) val=1; } else { if(a[i]!=val) { //cout<<i<<' '<<a[i]<<' '<<val<<endl; return 0; } else { val=a[i]+1; if(val==n+1) val=1; } } } else { if(hp[a[i]]) return 0; hp[a[i]]=1; if(val!=-1) { val++; if(val==n+1) val=1; } } } return 1; } //---------------------- int replacement(int n, int gondolaSeq[], int replacementSeq[]) { int rep=1, m=0; for(int i=0; i<n; i++) { if(gondolaSeq[i]<=n) { rep=gondolaSeq[i]; for(int j=i-1; j>=0; j--) { rep--; if(!rep) rep=n; } break; } } multiset<pair<int, int>> s; for(int i=0; i<n; i++) { if(rep!=gondolaSeq[i]) { s.insert({gondolaSeq[i], rep}); } rep++; if(rep==n+1) rep=1; } int b=n+1; while(s.size()) { int need=(*s.begin()).first; int hv=(*s.begin()).second; s.erase(s.begin()); replacementSeq[m++]=hv; hv=b++; if(need!=hv) s.insert({need, hv}); } return m; } //---------------------- ll pw(ll b, ll p) { if(!p) return 1; ll P=pw(b, p>>1); if(p&1) return P*P%MOD*b%MOD; return P*P%MOD; } int countReplacement(int n, int inputSeq[]) { //cout<<"ok"<<endl<<flush; if(!valid(n, inputSeq)) return 0; //cout<<"ok"<<endl<<flush; ll rep=-1; for(int i=0; i<n; i++) { if(inputSeq[i]<=n) { rep=inputSeq[i]; for(int j=i-1; j>=0; j--) { rep--; if(!rep) rep=n; } break; } } ll ans=1; if(rep==-1) { rep=1; ans*=n; } multiset<pair<ll, ll>> s; for(int i=0; i<n; i++) { if(rep!=inputSeq[i]) { s.insert({inputSeq[i], rep}); } rep++; if(rep==n+1) rep=1; } ll b=n+1; while(s.size()) { ll need=(*s.begin()).first; ll hv=(*s.begin()).second; //cout<<s.size()<<' '<<need<<' '<<hv<<' '<<endl<<flush; if(need!=hv) ans*=pw(1ll*s.size(), need-b), ans%=MOD; b=need+1; s.erase(s.begin()); } return ans; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...