#include "gondola.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int valid(int n, int inputSeq[]) {
// trova la posizione dell'elemento 1
int pos = -1;
for(int i = 0; i < n; ++i) {
if(inputSeq[i] <= n) {
if(pos == -1) {
pos = (n + i - inputSeq[i]) % n + 1;
} else {
if(inputSeq[i] != ((n + i - pos) % n + 1)) return false;
}
}
}
// conta le inversioni
unordered_map<int, int> occ;
for(int i = 0; i < n; ++i) {
occ[inputSeq[i]]++;
}
// controlla non ci siano duplicati
return all_of(occ.begin(), occ.end(), [](pair<const int, int>& x){return x.second == 1;});
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
// if(!valid(n, gondolaSeq)) {}
int* mx = max_element(gondolaSeq, gondolaSeq + n);
int k = *mx - n;
// setta tutto a -1
for(int i = 0; i < k; ++i) {
replacementSeq[i] = -1;
}
// trova la posizione dell'elemento 1
int pos = 0;
for(int i = 0; i < n; ++i) {
if(gondolaSeq[i] <= n) {
pos = (n + i - gondolaSeq[i] + 1) % n;
break;
}
}
// imposta a partire da gondolaSeq
for(int i = 0; i < n; ++i) {
if(gondolaSeq[i] > n) {
replacementSeq[gondolaSeq[i] - n - 1] = (n + i - pos) % n + 1;
}
}
int curr_mx = (mx - gondolaSeq + n - pos) % n + 1;
for(int i = 0; i < k; ++i) {
if(replacementSeq[i] == -1) {
replacementSeq[i] = curr_mx;
curr_mx = n + i + 1;
}
}
replacementSeq[*mx - n - 1] = curr_mx;
return k;
}
//----------------------
using ll = long long;
const ll mod = 1000000009ll;
ll mod_pow(ll base, ll exp) {
ll ans = 1;
while(exp > 0) {
if(exp & 1) ans = (ans * base) % mod;
exp >>= 1;
base = (base * base) % mod;
}
return ans;
}
int countReplacement(int n, int inputSeq[]) {
if(!valid(n, inputSeq)) return 0;
ll ans = 1;
vector<ll> s;
for(int i = 0; i < n; ++i) {
if(inputSeq[i] > n)
s.push_back(inputSeq[i]);
}
sort(s.begin(), s.end());
ll k = s.size();
if(k == 0)
return 1;
ans = (ans * mod_pow(k, s[0] - n - 1)) % mod;
for(ll i = 1; i < k; ++i) {
ans = (ans * mod_pow(k - i, s[i] - s[i-1] - 1)) % mod;
}
// tutte le sequenze possibili ruotando l'array
if(k == n) {
ans = (ans * n) % mod;
}
return ans;
}
# |
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 |
1 ms |
212 KB |
Output is correct |
5 |
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 |
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 |
7 ms |
1968 KB |
Output is correct |
7 |
Correct |
8 ms |
596 KB |
Output is correct |
8 |
Correct |
12 ms |
3428 KB |
Output is correct |
9 |
Correct |
5 ms |
1492 KB |
Output is correct |
10 |
Correct |
15 ms |
3884 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 |
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 |
7 ms |
1968 KB |
Output is correct |
7 |
Correct |
8 ms |
640 KB |
Output is correct |
8 |
Correct |
13 ms |
3396 KB |
Output is correct |
9 |
Correct |
5 ms |
1492 KB |
Output is correct |
10 |
Correct |
16 ms |
3812 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
7 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
8 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 |
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 |
# |
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 |
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 |
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 |
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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 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 |
11 |
Correct |
8 ms |
596 KB |
Output is correct |
12 |
Correct |
9 ms |
660 KB |
Output is correct |
13 |
Correct |
10 ms |
1084 KB |
Output is correct |
14 |
Correct |
8 ms |
592 KB |
Output is correct |
15 |
Correct |
18 ms |
2004 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 |
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 |
# |
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 |
1 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 |
22 ms |
3528 KB |
Output is correct |
10 |
Correct |
17 ms |
3004 KB |
Output is correct |
11 |
Correct |
7 ms |
1508 KB |
Output is correct |
12 |
Correct |
8 ms |
1584 KB |
Output is correct |
13 |
Correct |
2 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 |
23 ms |
3536 KB |
Output is correct |
10 |
Correct |
16 ms |
3108 KB |
Output is correct |
11 |
Correct |
6 ms |
1492 KB |
Output is correct |
12 |
Correct |
7 ms |
1584 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
32 ms |
4616 KB |
Output is correct |
15 |
Correct |
39 ms |
6200 KB |
Output is correct |
16 |
Correct |
6 ms |
1188 KB |
Output is correct |
17 |
Correct |
23 ms |
3812 KB |
Output is correct |
18 |
Correct |
12 ms |
2224 KB |
Output is correct |