#include "gondola.h"
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
int valid(int n, int inputSeq[])
{
bool acc = 1;
set<int> s;
for(int i = 0 ; i< n ; i++)
{
inputSeq[i]--;
s.insert(inputSeq[i]);
if(inputSeq[i] < n)
acc = 0;
}
if((int)s.size() != n)
return 0;
if(acc)
return 1;
for(int i = 0 ; i < n ; i++)
{
if(inputSeq[i] < n)
{
int j = i;
int ok = 1;
do {
j = (j + 1) % n;
if (inputSeq[j] >= n)
continue;
if (inputSeq[j] != (inputSeq[i] + j - i + n) % n) {
ok = 0;
break;
}
}
while(i != j);
return ok;
}
}
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
vector<pair<int, int>> s;
vector<int> perm(n);
for(int i = 0 ; i < n ; i++)
perm[i] = i;
for(int i = 0 ; i < n ; i++)
gondolaSeq[i]--;
for(int i = 0 ; i < n ; i++)
{
if(gondolaSeq[i] >= n)
continue;
perm[i] = gondolaSeq[i];
int j = i;
do
{
j = (j + 1)%n;
perm[j] = (gondolaSeq[i] + j - i + n)%n;
} while (i != j);
break;
}
for(int i = 0 ; i < n ; i++)
{
if(gondolaSeq[i] >= n)
s.push_back({gondolaSeq[i] , i});
}
sort(s.begin() , s.end());
int t = 0;
int cnt = n - 1;
for(auto [x , i] : s)
{
int last = perm[i];
while (cnt < x)
{
cnt++;
replacementSeq[t++] = last + 1;
assert(t <= 250000);
last = cnt;
}
}
return t;
}
//----------------------
const int M = 1e9 + 9;
long long bpow(int x , int p)
{
if(p == 0)
return 1;
long long u = bpow(x , p/2);
u = (u * u)%M;
if(p%2)
u = (u * x)%M;
return u;
}
int countReplacement(int n, int gondolaSeq[])
{
vector<pair<int, int>> s;
vector<int> all(n);
vector<int> perm(n);
for(int i = 0 ; i < n ; i++)
perm[i] = i;
for(int i = 0 ; i < n ; i++)
{
gondolaSeq[i]--;
all[i] = gondolaSeq[i];
}
sort(all.begin() , all.end());
all.erase(unique(all.begin() , all.end()) , all.end());
if((int)all.size() != n)
return 0;
for(int i = 0 ; i < n ; i++)
{
if(gondolaSeq[i] >= n)
continue;
perm[i] = gondolaSeq[i];
int j = i;
do
{
j = (j + 1)%n;
if(gondolaSeq[j] < n && gondolaSeq[j] != (gondolaSeq[i] + j - i + n)%n)
return 0;
perm[j] = (gondolaSeq[i] + j - i + n)%n;
} while (i != j);
break;
}
int t = 0;
for(int i = 0 ; i < n ; i++)
{
if(gondolaSeq[i] >= n)
{
t++;
s.push_back({gondolaSeq[i] , i});
}
}
long long nbWays =1;
if(t == n)
nbWays = n;
sort(s.begin() , s.end());
int cnt = n - 1;
for(auto [x , i] : s)
{
int k = x - cnt - 1;
nbWays = (nbWays * bpow(t , k))%M;
cnt = x;
t--;
}
return (int)nbWays;
}
Compilation message
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:10:14: warning: control reaches end of non-void function [-Wreturn-type]
10 | set<int> s;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
2140 KB |
Output is correct |
7 |
Correct |
20 ms |
3696 KB |
Output is correct |
8 |
Correct |
15 ms |
3920 KB |
Output is correct |
9 |
Correct |
4 ms |
1372 KB |
Output is correct |
10 |
Correct |
17 ms |
4580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
2140 KB |
Output is correct |
7 |
Correct |
20 ms |
3600 KB |
Output is correct |
8 |
Correct |
13 ms |
3932 KB |
Output is correct |
9 |
Correct |
5 ms |
1372 KB |
Output is correct |
10 |
Correct |
17 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
10 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
26 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
988 KB |
Output is correct |
12 |
Correct |
6 ms |
1368 KB |
Output is correct |
13 |
Correct |
9 ms |
1380 KB |
Output is correct |
14 |
Correct |
5 ms |
860 KB |
Output is correct |
15 |
Correct |
12 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
16 ms |
2264 KB |
Output is correct |
10 |
Correct |
11 ms |
2196 KB |
Output is correct |
11 |
Correct |
5 ms |
1116 KB |
Output is correct |
12 |
Correct |
6 ms |
1240 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
14 ms |
2308 KB |
Output is correct |
10 |
Correct |
12 ms |
2008 KB |
Output is correct |
11 |
Correct |
5 ms |
1116 KB |
Output is correct |
12 |
Correct |
6 ms |
1240 KB |
Output is correct |
13 |
Correct |
2 ms |
508 KB |
Output is correct |
14 |
Correct |
19 ms |
3284 KB |
Output is correct |
15 |
Correct |
21 ms |
3540 KB |
Output is correct |
16 |
Correct |
4 ms |
1112 KB |
Output is correct |
17 |
Correct |
21 ms |
2264 KB |
Output is correct |
18 |
Correct |
10 ms |
1752 KB |
Output is correct |