#include "gondola.h"
#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
#include <set>
typedef long long llong;
const int MAXNUM = 250000 + 10;
const int MAXN = 100000 + 10;
const int MOD = 1e9 + 9;
const int INF = 1e9;
std::set <int> s;
llong power(llong num, llong base)
{
if (base == 0)
{
return 1;
}
if (base & 1)
{
return (num * power(num, base - 1)) % MOD;
}
llong res = power(num, base / 2);
return (res * res) % MOD;
}
int n;
int getPosDiff(int last, int curr)
{
if (last > curr)
{
return n - last + curr;
}
return curr - last;
}
int valid(int N, int inputSeq[])
{
n = N;
int minPos = 0;
for (int i = 1 ; i < n ; ++i)
{
if (s.count(inputSeq[i]))
{
return 0;
}
s.insert(inputSeq[i]);
if (inputSeq[i] < inputSeq[minPos])
{
minPos = i;
}
}
if (inputSeq[minPos] > n)
{
return 1;
}
for (int i = minPos - 1 ; i >= std::max(0, minPos - inputSeq[minPos] + 1) ; --i)
{
if (inputSeq[i] <= n)
{
return 0;
}
}
if (minPos + 1 < inputSeq[minPos])
{
for (int i = n - 1 ; i >= n + minPos + 1 - inputSeq[minPos] ; --i)
{
if (inputSeq[i] <= n)
{
return 0;
}
}
}
int lastPos = minPos;
int last = inputSeq[minPos];
for (int i = minPos + 1 ; i < n ; ++i)
{
if (inputSeq[i] <= n)
{
if (last > inputSeq[i])
{
return 0;
}
if (getPosDiff(lastPos, i) != inputSeq[i] - last)
{
return 0;
}
last = inputSeq[i];
lastPos = i;
}
}
for (int i = 1 ; i < minPos ; ++i)
{
if (inputSeq[i] <= n)
{
if (last > inputSeq[i])
{
return 0;
}
if (getPosDiff(lastPos, i) != inputSeq[i] - last)
{
return 0;
}
last = inputSeq[i];
lastPos = i;
}
}
return 1;
}
//----------------------
int a[MAXN];
int res[MAXNUM];
int sorted[MAXN];
int replacement(int N, int gondolaSeq[], int replacementSeq[])
{
n = N;
int minPos = 0;
for (int i = 1 ; i < n ; ++i)
{
if (gondolaSeq[i] < gondolaSeq[minPos])
{
minPos = i;
}
}
int cnt = gondolaSeq[minPos];
if (cnt > n) cnt = 1;
for (int i = minPos ; i < n ; ++i)
{
a[i] = cnt++;
if (cnt == n + 1)
{
cnt = 1;
}
}
for (int i = 0 ; i < minPos ; ++i)
{
a[i] = cnt++;
if (cnt == n + 1)
{
cnt = 1;
}
}
std::iota(sorted, sorted + n, 0);
std::sort(sorted, sorted + n, [&](int x, int y)
{
return gondolaSeq[x] < gondolaSeq[y];
});
int len = 0, reached = n;
for (int i = 0 ; i < n ; ++i)
{
if (gondolaSeq[sorted[i]] <= reached)
{
assert(gondolaSeq[sorted[i]] <= n);
continue;
}
replacementSeq[len++] = a[sorted[i]];
reached++;
while (gondolaSeq[sorted[i]] > reached)
{
replacementSeq[len++] = reached;
reached++;
}
}
return len;
}
//----------------------
int countReplacement(int N, int inputSeq[])
{
if (!valid(N, inputSeq))
{
return 0;
}
n = N;
std::vector <int> vals;
for (int i = 0 ; i < n ; ++i)
{
if (inputSeq[i] > n)
{
vals.push_back(inputSeq[i]);
}
}
int last = n;
llong ans = 1;
std::sort(vals.begin(), vals.end());
for (int i = 0 ; i < vals.size() ; ++i)
{
ans *= power(vals.size() - i, vals[i] - last - 1);
last = vals[i];
ans %= MOD;
}
if (vals.size() == n)
{
ans *= n;
ans %= MOD;
}
return ans;
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:217:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
217 | for (int i = 0 ; i < vals.size() ; ++i)
| ~~^~~~~~~~~~~~~
gondola.cpp:224:21: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
224 | if (vals.size() == n)
| ~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
10 ms |
2192 KB |
Output is correct |
7 |
Correct |
7 ms |
592 KB |
Output is correct |
8 |
Correct |
19 ms |
3892 KB |
Output is correct |
9 |
Correct |
6 ms |
1364 KB |
Output is correct |
10 |
Correct |
24 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
288 KB |
Output is correct |
6 |
Correct |
10 ms |
2192 KB |
Output is correct |
7 |
Correct |
7 ms |
596 KB |
Output is correct |
8 |
Correct |
19 ms |
3868 KB |
Output is correct |
9 |
Correct |
6 ms |
1360 KB |
Output is correct |
10 |
Correct |
32 ms |
4464 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
11 ms |
2004 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
29 ms |
4616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
8 ms |
1236 KB |
Output is correct |
12 |
Correct |
13 ms |
1364 KB |
Output is correct |
13 |
Correct |
11 ms |
1364 KB |
Output is correct |
14 |
Correct |
7 ms |
1236 KB |
Output is correct |
15 |
Correct |
16 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
39 ms |
4308 KB |
Output is correct |
10 |
Correct |
24 ms |
3796 KB |
Output is correct |
11 |
Correct |
9 ms |
1620 KB |
Output is correct |
12 |
Correct |
10 ms |
1876 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
40 ms |
4368 KB |
Output is correct |
10 |
Correct |
24 ms |
3868 KB |
Output is correct |
11 |
Correct |
8 ms |
1672 KB |
Output is correct |
12 |
Correct |
10 ms |
1948 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
37 ms |
5960 KB |
Output is correct |
15 |
Correct |
42 ms |
6592 KB |
Output is correct |
16 |
Correct |
7 ms |
1532 KB |
Output is correct |
17 |
Correct |
27 ms |
4536 KB |
Output is correct |
18 |
Correct |
18 ms |
2860 KB |
Output is correct |