#include "gondola.h"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 300010;
const long long int MOD = 1e9 + 9;
int correct[MAXN], pos[MAXN];
bool all = false;
map<int, bool> marc;
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);
int ans = 1;
for(int i = 0; i < n; i++)
{
if(seq[i] <= n && correct[i] != seq[i]) ans = 0;
if(seq[i] > n && marc[seq[i]]) ans = 0;
marc[seq[i]] = true;
}
return ans;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
calc(n, gondolaSeq);
for(int i = 0; i < 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, long long int exp)
{
if(exp == 0) return 1;
if(exp == 1) return num;
long long int aux = fe(num, exp / 2);
aux *= aux, aux %= MOD;
if(exp % 2) return (aux * num) % MOD;
return aux % MOD;
}
vector<long long int> v;
int countReplacement(int n, int inputSeq[])
{
if(!valid(n, inputSeq)) return 0;
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, all = false;
v.clear();
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));
}
*/
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:102:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++)
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
16 ms |
2432 KB |
Output is correct |
7 |
Correct |
34 ms |
4088 KB |
Output is correct |
8 |
Correct |
26 ms |
4224 KB |
Output is correct |
9 |
Correct |
11 ms |
1536 KB |
Output is correct |
10 |
Correct |
30 ms |
4864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
15 ms |
2432 KB |
Output is correct |
7 |
Correct |
38 ms |
4088 KB |
Output is correct |
8 |
Correct |
29 ms |
4224 KB |
Output is correct |
9 |
Correct |
11 ms |
1536 KB |
Output is correct |
10 |
Correct |
30 ms |
4864 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
23 ms |
2304 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
45 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1536 KB |
Output is correct |
2 |
Correct |
5 ms |
1536 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
4 |
Correct |
5 ms |
1536 KB |
Output is correct |
5 |
Correct |
5 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1536 KB |
Output is correct |
2 |
Correct |
5 ms |
1536 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
4 |
Correct |
5 ms |
1536 KB |
Output is correct |
5 |
Correct |
9 ms |
1536 KB |
Output is correct |
6 |
Correct |
5 ms |
1536 KB |
Output is correct |
7 |
Correct |
6 ms |
1536 KB |
Output is correct |
8 |
Correct |
5 ms |
1536 KB |
Output is correct |
9 |
Correct |
6 ms |
1536 KB |
Output is correct |
10 |
Correct |
5 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1536 KB |
Output is correct |
2 |
Correct |
5 ms |
1536 KB |
Output is correct |
3 |
Correct |
5 ms |
1536 KB |
Output is correct |
4 |
Correct |
5 ms |
1536 KB |
Output is correct |
5 |
Correct |
6 ms |
1536 KB |
Output is correct |
6 |
Correct |
5 ms |
1536 KB |
Output is correct |
7 |
Correct |
5 ms |
1536 KB |
Output is correct |
8 |
Correct |
5 ms |
1536 KB |
Output is correct |
9 |
Correct |
6 ms |
1536 KB |
Output is correct |
10 |
Correct |
6 ms |
1536 KB |
Output is correct |
11 |
Correct |
15 ms |
2176 KB |
Output is correct |
12 |
Correct |
16 ms |
2176 KB |
Output is correct |
13 |
Correct |
18 ms |
2432 KB |
Output is correct |
14 |
Correct |
15 ms |
2048 KB |
Output is correct |
15 |
Correct |
25 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
49 ms |
4980 KB |
Output is correct |
10 |
Correct |
40 ms |
4344 KB |
Output is correct |
11 |
Correct |
17 ms |
1920 KB |
Output is correct |
12 |
Correct |
20 ms |
2048 KB |
Output is correct |
13 |
Correct |
8 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
49 ms |
4980 KB |
Output is correct |
10 |
Correct |
39 ms |
4344 KB |
Output is correct |
11 |
Correct |
17 ms |
1920 KB |
Output is correct |
12 |
Correct |
21 ms |
2304 KB |
Output is correct |
13 |
Correct |
8 ms |
768 KB |
Output is correct |
14 |
Correct |
63 ms |
6132 KB |
Output is correct |
15 |
Correct |
77 ms |
7668 KB |
Output is correct |
16 |
Correct |
15 ms |
1920 KB |
Output is correct |
17 |
Correct |
47 ms |
5116 KB |
Output is correct |
18 |
Correct |
27 ms |
3328 KB |
Output is correct |