# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
852311 | 2023-09-21T15:00:18 Z | DobromirAngelov | Gondola (IOI14_gondola) | C++14 | 0 ms | 0 KB |
#include "gondola.h" #include<bits/stdc++.h> using namespace std; const int MAXN=1e5+5; const int MOD=1e9+9; map<int,int> m; int valid(int n, int a[]) { for(int i=0;i<n;i++) { if(m[a[i]]!=0) return 0; m[a[i]]=1; } int mn=1e9, idx=-1; for(int i=0;i<n;i++) { if(a[i]<mn) { mn=a[i]; idx=i; } } if(mn<=n) { int cur=mn+1; for(int i=idx+1;i!=idx;i++) { if(i>=n) i-=n; if(a[i]<=n && a[i]!=cur) return 0; cur++; if(i==n-1) i=-1; } } return 1; } int b[MAXN]; vector<pair<int,int> > v; int replacement(int n, int a[], int replacementSeq[]) { int mn=1e9,idx=-1; for(int i=0;i<n;i++) { if(a[i]<mn) { mn=a[i]; idx=i; } } if(mn>n) { for(int i=0;i<n;i++) b[i]=i+1; } else { int cur=mn; b[idx]=cur++; for(int i=idx+1;i!=idx;i++) { if(i>=n) i-=n; if(cur>n) cur-=n; b[i]=cur++; if(i==n-1) i=-1; } } for(int i=0;i<n;i++) { if(a[i]>n) v.push_back({a[i],b[i]}); } sort(v.begin(), v.end()); int ptr=0; int cur=n; for(int i=0;i<v.size();i++) { replacementSeq[ptr++]=v[i].second; cur++; while(cur<v[i].first) { replacementSeq[ptr++]=cur++; } } return ptr; } int fastPow(int a,int pw) { long long ret=1; while(pw>0) { if(pw&1) ret=ret*a%MOD; a=(long long) a*a%MOD; pw/=2; } return ret; } vector<int> v2; int countReplacement(int n, int a[]) { if(!valid(n,a)) return 0; for(int i=0;i<n;i++) { if(a[i]>n) v2.push_back(a[i]); } v2.push_back(n); sort(v2.begin(), v2.end()); long long ans=1; for(int i=1;i<v2.size();i++) { ans=ans*fastPow(v2.size()-i, v2[i]-v2[i-1])%MOD; } if(mn>n) ans=ans*n%MOD; return ans; }