# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
165044 | kostia244 | Gondola (IOI14_gondola) | C++17 | 88 ms | 10352 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "gondola.h"
#include<bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<int>;
int valid(int n, int a[]) {
set<int> x;
int p = 0;
for(int i = 0; i < n; i++) {
if(!x.insert(a[i]).second) return 0;
if(a[p]>a[i]) p = i;
}
if(p>n) return true;
int u = a[p];
for(int i = 0; i < n; i++) {
if(a[p]<=n&&a[p]!=u) return 0;
if(a[p]>n)
a[p]=u;
p = (p+1)%n;
u = (u%n)+1;
}
return 1;
}
//----------------------
int pos[4*250250];
int replacement(int n, int a[], int b[]) {
memset(pos, -1, sizeof pos);
int mp = 0, p = 0;
for(int i = 0; i < n; i++) {
if(a[i] > n) {
pos[a[i]] = i;
}
if(a[i]>a[mp]) mp=i;
if(a[i]<a[p]) p=i;
}
if(a[mp]<=n) return 0;
int M = a[mp], l = 0;
if(a[p]>n) a[p] = 1;
int u = a[p];
for(int i = 0; i < n; i++) {
a[p] = u;
p = (p+1)%n;
u = (u%n)+1;
}
for(int i = n+1; i <= M; i++) {
if(pos[i]!=-1) {
b[l++] = a[pos[i]];
a[pos[i]] = i;
} else {
b[l++] = a[mp];
a[mp] = i;
}
}
return l;
}
//----------------------
ll mod = 1e9+9;
ll bp(ll a, ll p) {
ll r = 1;
while(p) {
if(p&1) r = (r*a)%mod;
a = (a*a)%mod;
p>>=1;
}
return r;
}
int countReplacement(int n, int a[]) {
ll ans = 1, use = 0;
memset(pos, -1, sizeof pos);
int mp = 0, p = 0;
vi useful;
for(int i = 0; i < n; i++) {
if(a[i] > n) {
useful.pb(a[i]);
use++;
}
if(a[i]>a[mp]) mp=i;
if(a[i]<a[p]) p=i;
}
int M = a[mp], l = 0, GG = 0;
if(a[p]>n) a[p] = 1, GG = 1;
if(a[mp]<=n) return 1;
if(!valid(n, a)) return 0;
p = n+1;
sort(all(useful));
for(auto i : useful) {
ans = (ans*bp(use, i-p))%mod;
p = i+1;
use--;
}
if(GG) (ans *= n)%=mod;
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |