#include <bits/stdc++.h>
#include "gondola.h"
#define ll long long
using namespace std;
int valid(int n, int inputSeq[]) {
unordered_map<int, int> cnt;
for (int i = 0; i < n; ++i) cnt[inputSeq[i]]++;
if ((int)cnt.size() < n) return 0;
for (int i = 1; inputSeq[0]+i <= n; ++i) {
if (inputSeq[i] <= n && inputSeq[i] != inputSeq[0]+i) return 0;
}
for (int i = 1; inputSeq[0]-i > 0; ++i) {
if (inputSeq[n-i] <= n && inputSeq[n-i] != inputSeq[0]-i) return 0;
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
vector<int> gondola(n);
int idx = -1;
for (int i = 0; i < n; ++i) {
if (gondolaSeq[i] <= n) {
gondola[i] = gondolaSeq[i];
idx = i;
break;
}
}
if (idx == -1) {
for (int i = 0; i < n; ++i) gondola[i] = i+1;
} else {
for (int i = 1; gondola[idx]+i <= n; ++i) {
gondola[(idx+i)%n] = gondola[idx]+i;
}
for (int i = 1; gondola[idx]-i > 0; ++i) {
gondola[(idx-i+n)%n] = gondola[idx]-i;
}
}
vector<pair<int, int>> replacements;
replacements.reserve(n);
for (int i = 0; i < n; ++i) {
if (gondolaSeq[i] != gondola[i]) replacements.push_back({gondolaSeq[i], gondola[i]});
}
sort(replacements.begin(), replacements.end());
int l = 0, new_gondola = n+1;
for (pair<int, int> p: replacements) {
while (p.first > new_gondola) {
replacementSeq[l] = p.second;
p.second = new_gondola;
++new_gondola; ++l;
}
replacementSeq[l] = p.second;
++new_gondola;
++l;
}
return l;
}
const int mod = 1e9+7;
ll mpow(ll a, int p) {
if (p == 0) return 1;
ll t = mpow(a, p/2);
if (p%2) return (((t*t)%mod)*a)%mod;
return (t*t)%mod;
}
int countReplacement(int n, int inputSeq[]) {
if (!valid(n, inputSeq)) return 0;
vector<int> g(n);
for (int i = 0; i < n; ++i) {
g[i] = inputSeq[i]-n;
}
sort(g.begin(), g.end());
ll ans = 1, cnt = 0;
bool def_order = false;
for (int i = 0; i < n; ++i) {
if (g[i] <= 0) {
def_order = true;
++cnt;
continue;
}
if (def_order) ans *= mpow(n-cnt, g[i]-1);
else ans *= mpow(n-cnt, g[i]);
ans %= mod;
++cnt;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
6 ms |
1964 KB |
Output is correct |
7 |
Correct |
13 ms |
3424 KB |
Output is correct |
8 |
Correct |
9 ms |
3424 KB |
Output is correct |
9 |
Correct |
4 ms |
1616 KB |
Output is correct |
10 |
Correct |
11 ms |
3936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
6 ms |
1964 KB |
Output is correct |
7 |
Correct |
14 ms |
3408 KB |
Output is correct |
8 |
Correct |
10 ms |
3384 KB |
Output is correct |
9 |
Correct |
4 ms |
1616 KB |
Output is correct |
10 |
Correct |
15 ms |
3936 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
9 ms |
1964 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
17 ms |
5472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
7 ms |
848 KB |
Output is correct |
12 |
Correct |
7 ms |
1104 KB |
Output is correct |
13 |
Correct |
13 ms |
1176 KB |
Output is correct |
14 |
Correct |
8 ms |
848 KB |
Output is correct |
15 |
Correct |
14 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |