# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
218864 | Kenzo_1114 | Gondola (IOI14_gondola) | C++17 | 0 ms | 0 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>
using namespace std;
const int MAXN = 100010;
const long long int MOD = 1e9 + 9;
int correct[MAXN], pos[3 * MAXN];
bool marc[3 * MAXN], all = false;
void calc(int n, int seq[])
{
int id = -1;
for(int i = 0; i < n; i++)
if(seq[i] <= n)
{ id = i; break; }
if(id != -1) correct[id] = seq[id];
else id = 0, correct[id] = 1, all = true;
for(int i = id + 1; i < n; i++)
{
correct[i] = correct[i - 1] + 1;
if(correct[i] > n) correct[i] = 1;
}
for(int i = id - 1; i >= 0; i--)
{
correct[i] = correct[i + 1] - 1;
if(correct[i] < 1) correct[i] = n;
}
}
int valid(int n, int seq[])
{
calc(n, seq);
for(int i = 0; i < n; i++)
{
if(seq[i] <= n && correct[i] != seq[i]) return 0;
if(seq[i] > n && marc[seq[i]]) return 0;
marc[seq[i]] = true;
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
calc(n, gondolaSeq);
for(int i = 0; i < 3 * MAXN; i++) pos[i] = -1;
int mx = n;
for(int i = 0; i < n; i++)
if(gondolaSeq[i] > n)
pos[gondolaSeq[i]] = i, mx = max(mx, gondolaSeq[i]);
for(int i = mx - 1; i > n; i--)
if(pos[i] == -1)
pos[i] = pos[i + 1];
int l = 0;
for(int i = n + 1; i <= mx; i++)
{
replacementSeq[l] = correct[pos[i]];
correct[pos[i]] = i;
l++;
}
return l;
}
long long int fe(long long int num, int exp)
{
if(exp == 0) return 1;
if(exp == 1) return num;
long long int aux = fe(num, exp / 2);
aux *= aux;
if(exp % 2) return aux % MOD;
return (aux * num) % MOD;
}
long long int countReplacement(int n, int inputSeq[])
{
if(!valid(n, inputSeq)) return 0;
vector<long long int> v;
for(int i = 0; i < n; i++)
if(inputSeq[i] > n)
v.push_back(inputSeq[i]);
sort(v.begin(), v.end());
long long int ans = 1;
long long int sz = v.size(), last = n;
for(int i = 0; i < v.size(); i++)
{
// printf("num = %lld exp = %lld\n", sz, v[i] - 1- last);
ans *= fe(sz, v[i] - 1 - last);
ans %= MOD;
last = v[i];
sz--;
}
if(all) ans *= n, ans %= MOD;
return ans;
}
/*
int N, SEQ[MAXN];
int main ()
{
scanf("%d", &N);
for(int i = 0; i < N; i++) scanf("%d", &SEQ[i]);
// printf("valid = %d\n", valid(N, SEQ));
printf("cont = %lld\n", countReplacement(N, SEQ));
}
*/