#include <bits/stdc++.h>
#include "gondola.h"
#define MOD 1000000009
using namespace std;
int init[250010];
map <int,int> f;
pair <int,int> w[250010];
int valid(int n, int v[]){
int i , found , poz , ok , pin , fi , space;
found = -1;
poz = 0;
ok = 0;
for (i = 0 ; i < n ; i++)
v[i]--;
for (i = 0 ; i < n ; i++){
if (f[v[i]])
return 0;
f[v[i]] = 1;
if (v[i] < n){
if (found == -1){
found = v[i];
poz = i;
pin = i;
fi = v[i];
}
else {
if (found > v[i]){
if (ok == 1)
return 0;
ok = 1;
space = n - 1 - found + v[i];
if (space != i - poz - 1)
return 0;
found = v[i];
poz = i;
}
else {
space = v[i] - found - 1;
if (space != i - poz - 1)
return 0;
found = v[i];
poz = i;
}
}
}
}
/// verif cu ult poz si prima poz
if (found == -1)
return 1;
if (found > fi){
if (ok == 1)
return 0;
ok = 1;
space = n - 1 - found + fi;
if (space != (n - 1) - poz + pin)
return 0;
}
else {
if (!ok)
return 0;
space = fi - found - 1;
if (space != (n - 1) - poz + pin)
return 0;
}
return 1;
}
/// prima parte e ok
int replacement(int n, int v[], int w[]){
int found , maxi , p , i , len;
found = 0;
maxi = 0;
p = 0;
for (i = 0 ; i < n ; i++){
f[v[i]] = i + 1;
if (v[i] > maxi){
maxi = v[i];
p = i;
}
if (v[i] <= n){
found = v[i];
}
if (found){
init[i] = found;
found++;
if (found == n + 1)
found = 1;
}
}
if (!found) /// nu ai gasit pana acum, iei o secventa random
found = 1;
for (i = 0 ; i < n && init[i] == 0 ; i++){
init[i] = found;
found++;
if (found == n + 1)
found = 1;
}
len = 0;
for (i = n + 1 ; i <= maxi ; i++){
if (f[i]){ /// am nevoie de i
w[len] = init[ f[i] - 1 ];
init[f[i] - 1] = i;
}
else {
w[len] = init[p];
init[p] = i;
}
len++;
}
return len;
}
/// a doua parte e ok
int ridput (int x , int y){
long long sol = 1;
while (y){
if (y % 2)
sol = (1LL * sol * x)%MOD;
x = (1LL * x * x)%MOD;
y /= 2;
}
return sol;
}
int countReplacement(int n, int v[]){
int is_ok , i , places , elem;
long long sol;
is_ok = valid (n , v);
if (!is_ok)
return 0;
places = n;
elem = 0;
sol = 1;
for (i = 0 ; i < n ; i++){
v[i]++;
f[v[i]] = i + 1;
if (v[i] <= n)
places--;
else w[++elem] = make_pair (v[i] , i);
}
sort (w + 1 , w + elem + 1);
w[0].first = n;
for (i = 1 ; i <= elem ; i++){
sol = (1LL * sol * ridput(places , w[i].first - w[i - 1].first - 1))%MOD;
places--;
}
if (elem == n)
sol = (1LL * sol * n)%MOD;
return sol;
}
Compilation message
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:83:15: warning: 'fi' may be used uninitialized in this function [-Wmaybe-uninitialized]
space = n - 1 - found + fi;
~~~~~~^~~~~~~~~~~~~~~~~~~~
gondola.cpp:97:36: warning: 'pin' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (space != (n - 1) - poz + pin)
~~~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
19 ms |
2304 KB |
Output is correct |
7 |
Correct |
17 ms |
768 KB |
Output is correct |
8 |
Correct |
33 ms |
3960 KB |
Output is correct |
9 |
Correct |
13 ms |
1536 KB |
Output is correct |
10 |
Correct |
38 ms |
4520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
19 ms |
2304 KB |
Output is correct |
7 |
Correct |
20 ms |
640 KB |
Output is correct |
8 |
Correct |
33 ms |
3968 KB |
Output is correct |
9 |
Correct |
12 ms |
1536 KB |
Output is correct |
10 |
Correct |
39 ms |
4600 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
10 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
17 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
640 KB |
Output is correct |
11 |
Correct |
31 ms |
4600 KB |
Output is correct |
12 |
Correct |
35 ms |
5240 KB |
Output is correct |
13 |
Correct |
43 ms |
4832 KB |
Output is correct |
14 |
Correct |
31 ms |
4600 KB |
Output is correct |
15 |
Correct |
65 ms |
10104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
72 ms |
6136 KB |
Output is correct |
10 |
Correct |
61 ms |
5112 KB |
Output is correct |
11 |
Correct |
23 ms |
2176 KB |
Output is correct |
12 |
Correct |
27 ms |
2560 KB |
Output is correct |
13 |
Correct |
9 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
74 ms |
6136 KB |
Output is correct |
10 |
Correct |
55 ms |
5112 KB |
Output is correct |
11 |
Correct |
23 ms |
2164 KB |
Output is correct |
12 |
Correct |
28 ms |
2560 KB |
Output is correct |
13 |
Correct |
9 ms |
896 KB |
Output is correct |
14 |
Correct |
101 ms |
7160 KB |
Output is correct |
15 |
Correct |
125 ms |
8952 KB |
Output is correct |
16 |
Correct |
21 ms |
1920 KB |
Output is correct |
17 |
Correct |
69 ms |
6136 KB |
Output is correct |
18 |
Correct |
36 ms |
3576 KB |
Output is correct |