#include "gondola.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 250000;
int const modulo = 1000000009;
std::map<int, int> frec;
int valid(int n, int inputSeq[])
{
for(int i = 0; i < n; i++)
if(inputSeq[i] <= n) {
std::rotate(inputSeq, inputSeq + (n + i - inputSeq[i] + 1) % n, inputSeq + n);
break;
}
for(int i = 0; i < n; i++) {
if(frec[inputSeq[i]] == 1)
return false;
frec[inputSeq[i]] = 1;
if(inputSeq[i] <= n && i + 1 != inputSeq[i])
return false;
}
return true;
}
//----------------------
int seen[1 + nmax];
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
for(int i = 0; i < n; i++)
if(gondolaSeq[i] <= n) {
std::rotate(gondolaSeq, gondolaSeq + (n + i - gondolaSeq[i] + 1) % n, gondolaSeq + n);
break;
}
int smax = 0;
for(int i = 0; i < n; i++)
smax = MAX(smax, gondolaSeq[i]);
int pos = 0;
for(int i = 0; i < n; i++)
if(smax == gondolaSeq[i])
pos = i + 1;
for(int i = 0;i < n; i++)
seen[gondolaSeq[i]] = 1 + i;
for(int i = n + 1; i <= smax; i++)
if(seen[i] == 0 || i == smax) {
replacementSeq[i - n - 1] = pos;
pos = i;
} else
replacementSeq[i - n - 1] = seen[i];
return smax - n;
}
//----------------------
int lgpow(int a, int b){
if(b == 0)
return 1;
else if(b == 1)
return a;
else {
int result = lgpow(a, b / 2);
if(b % 2 == 0)
return 1LL * result * result % modulo;
else
return 1LL * result * result % modulo * a % modulo;
}
}
int countReplacement(int n, int inputSeq[])
{
if(valid(n, inputSeq) == 0)
return 0;
else {
int centered = 0;
for(int i = 0; i < n; i++)
if(inputSeq[i] <= n) {
std::rotate(inputSeq, inputSeq + (n + i - inputSeq[i] + 1) % n, inputSeq + n);
centered = 1;
break;
}
std::vector<int> v;
for(int i = 0; i < n; i++)
if(n < inputSeq[i] )
v.push_back(inputSeq[i]);
std::sort(v.begin(), v.end());
int last = n;
int result = 1;
for(int i = 0; i < v.size(); i++){
result = 1LL * result * lgpow(v.size() - i, v[i] - 1 - last) % modulo;
last = v[i];
}
if(centered == 0)
return 1LL * n * result % modulo;
else
return result;
}
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:104:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++){
~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
21 ms |
2168 KB |
Output is correct |
7 |
Correct |
14 ms |
632 KB |
Output is correct |
8 |
Correct |
45 ms |
3964 KB |
Output is correct |
9 |
Correct |
12 ms |
1528 KB |
Output is correct |
10 |
Correct |
49 ms |
4600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
0 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
20 ms |
2296 KB |
Output is correct |
7 |
Correct |
13 ms |
632 KB |
Output is correct |
8 |
Correct |
41 ms |
3960 KB |
Output is correct |
9 |
Correct |
12 ms |
1528 KB |
Output is correct |
10 |
Correct |
49 ms |
4600 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
7 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
14 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
404 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
12 ms |
888 KB |
Output is correct |
12 |
Correct |
13 ms |
1116 KB |
Output is correct |
13 |
Correct |
14 ms |
1656 KB |
Output is correct |
14 |
Correct |
12 ms |
1016 KB |
Output is correct |
15 |
Correct |
22 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
60 ms |
4404 KB |
Output is correct |
10 |
Correct |
45 ms |
3832 KB |
Output is correct |
11 |
Correct |
17 ms |
1784 KB |
Output is correct |
12 |
Correct |
21 ms |
1912 KB |
Output is correct |
13 |
Correct |
6 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
59 ms |
4472 KB |
Output is correct |
10 |
Correct |
44 ms |
3832 KB |
Output is correct |
11 |
Correct |
17 ms |
1656 KB |
Output is correct |
12 |
Correct |
20 ms |
1912 KB |
Output is correct |
13 |
Correct |
6 ms |
760 KB |
Output is correct |
14 |
Correct |
72 ms |
6008 KB |
Output is correct |
15 |
Correct |
83 ms |
6776 KB |
Output is correct |
16 |
Correct |
14 ms |
1656 KB |
Output is correct |
17 |
Correct |
53 ms |
4604 KB |
Output is correct |
18 |
Correct |
28 ms |
2936 KB |
Output is correct |