#include <iostream>
#include <algorithm>
#include <vector>
#include "gondola.h"
#define MAX 250000
#define MOD 1000000007
using namespace std;
int mark[MAX + 10];
vector <pair <int, int>> v;
int valid(int n, int inputSeq[])
{
int posOrig = -1;
for (int i = 0; i < n; i++)
{
if (mark[inputSeq[i]] == 1)
return 0;
if (inputSeq[i] <= n)
posOrig = i;
mark[inputSeq[i]] = 1;
}
if (posOrig == -1)
return 1;
int pos = posOrig;
for (int i = 1; i <= n; i++)
{
if (inputSeq[pos] <= n && (inputSeq[pos] - inputSeq[posOrig] + n) % n != (pos - posOrig + n) % n)
return 0;
pos = (pos + 1) % n;
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
int posOrig = -1;
for (int i = 0; i < n; i++)
if (gondolaSeq[i] <= n)
posOrig = i;
if (posOrig == -1)
for (int i = 0; i < n; i++)
v.push_back({gondolaSeq[i], i + 1});
else
{
int pos = posOrig;
int g = gondolaSeq[posOrig];
for (int i = 1; i <= n; i++)
{
if (gondolaSeq[pos] > n)
v.push_back({gondolaSeq[pos], g});
pos = (pos + 1) % n;
if (g == n - 1)
g = n;
else
g = (g + 1) % n;
}
}
sort(v.begin(), v.end());
int l = 0, nn = n + 1;
for (auto it : v)
{
replacementSeq[l] = it.second;
l++;
nn++;
while (nn <= it.first)
{
replacementSeq[l] = nn - 1;
l++;
nn++;
}
}
return l;
}
int fastPow(int base, int exp)
{
int ans = 1;
while (exp != 0)
if (exp % 2 == 0)
{
exp = exp / 2;
base = 1LL * base * base % MOD;
}
else
{
exp--;
ans = 1LL * ans * base % MOD;
}
return ans;
}
int countReplacement(int n, int inputSeq[])
{
if (valid(n, inputSeq) == 0)
return 0;
int posOrig = -1;
for (int i = 0; i < n; i++)
if (inputSeq[i] <= n)
posOrig = i;
int ans;
if (posOrig == -1)
{
ans = n;
for (int i = 0; i < n; i++)
v.push_back({inputSeq[i], i + 1});
}
else
{
ans = 1;
int pos = posOrig;
int g = inputSeq[posOrig];
for (int i = 1; i <= n; i++)
{
if (inputSeq[pos] > n)
v.push_back({inputSeq[pos], g});
pos = (pos + 1) % n;
if (g == n - 1)
g = n;
else
g = (g + 1) % n;
}
}
sort(v.begin(), v.end());
int cntGreater = v.size(), aux = n;
for (auto it : v)
{
ans = 1LL * ans * fastPow(cntGreater, it.first - aux - 1) % MOD;
cntGreater--;
aux = it.first;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
4 ms |
540 KB |
Output is correct |
7 |
Correct |
7 ms |
980 KB |
Output is correct |
8 |
Correct |
6 ms |
852 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
8 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
256 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 |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
596 KB |
Output is correct |
7 |
Correct |
7 ms |
980 KB |
Output is correct |
8 |
Correct |
7 ms |
852 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
8 ms |
848 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
4 ms |
1156 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
7 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
7 ms |
596 KB |
Output is correct |
12 |
Correct |
10 ms |
580 KB |
Output is correct |
13 |
Correct |
11 ms |
1488 KB |
Output is correct |
14 |
Correct |
7 ms |
596 KB |
Output is correct |
15 |
Correct |
16 ms |
2496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
Incorrect |
0 ms |
304 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |