#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
typedef long long int ll;
const ll MOD = 1000000009;
const int MAXN = 1e5 + 10;
ll expect[MAXN];
map<ll, int> marc;
ll mult(ll a, ll b)
{
a %= MOD; b %= MOD;
return ((a * b) % MOD);
}
ll exp(ll x, ll n)
{
x %= MOD; n %= MOD;
if (n == 0LL) return 1LL;
if (n == 1LL) return x;
if (n % 2LL) return mult(x, exp(x, n-1LL));
return exp(mult(x, x), n/2LL);
}
int valid(int n, int inputSeq[])
{
for (int i = 0; i < n; i++)
{
if (marc[inputSeq[i]]) return 0;
marc[inputSeq[i]] = 1;
}
bool Achei = false;
for (int i = 0; i < n; i++)
{
if (!Achei && inputSeq[i] > n) continue;
else if (!Achei && inputSeq[i] <= n)
{
expect[i] = inputSeq[i];
Achei = true;
}
else if (Achei) expect[i] = (expect[i-1]+1LL);
if (expect[i] > n) expect[i] -= n;
}
if (!expect[0]) expect[0] = expect[n-1]+1LL;
if (expect[0] > n) expect[0] -= n;
for (int i = 1; i < n && !expect[i]; i++)
{
expect[i] = expect[i-1]+1LL;
if (expect[i] > n) expect[i] -= n;
}
for (int i = 0; i < n; i++)
{
if (inputSeq[i] > n) continue;
if (expect[i] != inputSeq[i]) return 0;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
if (!valid(n, gondolaSeq)) return 0;
int tam = 0;
ll atual = n+1;
set<tuple<ll, ll, ll> > fora;
for (int i = 0; i < n; i++) if (gondolaSeq[i] > n) fora.emplace(gondolaSeq[i], expect[i], i);
while (!fora.empty())
{
auto [quero, tenho, id] = *fora.begin();
fora.erase(fora.begin());
replacementSeq[tam++] = tenho;
if (atual < quero) fora.emplace(quero, atual, id);
atual++;
}
return tam;
}
//----------------------
int countReplacement(int n, int inputSeq[])
{
// cerr << "ABA" << '\n';
if (!valid(n, inputSeq)) return 0;
set<ll> fora;
// cerr << "CABA" << '\n';
for (int i = 0; i < n; i++) if (inputSeq[i] > n) fora.emplace(inputSeq[i]);
ll atual = n+1;
ll resp = 1LL;
if (fora.size() == n) resp = n;
// cerr << "SABA" << '\n';
while (!fora.empty())
{
resp = mult(resp, exp((long long)fora.size(), *fora.begin() - atual));
atual = (*fora.begin());
atual++;
assert(resp > 0);
assert(atual > 0);
// cerr << atual << '\n';
fora.erase(fora.begin());
}
// cerr << "VABA" << '\n';
return (int)(resp % MOD);
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:96:21: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
96 | if (fora.size() == n) resp = n;
| ~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
14 ms |
3152 KB |
Output is correct |
7 |
Correct |
8 ms |
724 KB |
Output is correct |
8 |
Correct |
27 ms |
5712 KB |
Output is correct |
9 |
Correct |
8 ms |
2004 KB |
Output is correct |
10 |
Correct |
36 ms |
6600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
13 ms |
3168 KB |
Output is correct |
7 |
Correct |
9 ms |
724 KB |
Output is correct |
8 |
Correct |
30 ms |
5708 KB |
Output is correct |
9 |
Correct |
8 ms |
2088 KB |
Output is correct |
10 |
Correct |
33 ms |
6528 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
15 ms |
3016 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
38 ms |
6800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 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 |
11 |
Correct |
29 ms |
6180 KB |
Output is correct |
12 |
Correct |
35 ms |
7024 KB |
Output is correct |
13 |
Correct |
31 ms |
5452 KB |
Output is correct |
14 |
Correct |
28 ms |
6180 KB |
Output is correct |
15 |
Correct |
33 ms |
4288 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
304 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
50 ms |
8568 KB |
Output is correct |
10 |
Correct |
37 ms |
6896 KB |
Output is correct |
11 |
Correct |
14 ms |
3028 KB |
Output is correct |
12 |
Correct |
18 ms |
3632 KB |
Output is correct |
13 |
Correct |
4 ms |
1096 KB |
Output is correct |
# |
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 |
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 |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
50 ms |
8780 KB |
Output is correct |
10 |
Correct |
39 ms |
7156 KB |
Output is correct |
11 |
Correct |
14 ms |
3156 KB |
Output is correct |
12 |
Correct |
20 ms |
3560 KB |
Output is correct |
13 |
Correct |
4 ms |
1112 KB |
Output is correct |
14 |
Correct |
67 ms |
11256 KB |
Output is correct |
15 |
Correct |
77 ms |
12580 KB |
Output is correct |
16 |
Correct |
12 ms |
2516 KB |
Output is correct |
17 |
Correct |
52 ms |
8544 KB |
Output is correct |
18 |
Correct |
25 ms |
4948 KB |
Output is correct |