#include "gondola.h"
#include<bits/stdc++.h>
using namespace std;
int valid(int n, int inputSeq[]){
set<int> s;
bool works = true;
int idx = 0;
for(int i = 0; i < n; i++){
s.insert(inputSeq[i]);
if(inputSeq[i] <= n){
idx = i;
works = false;
}
}
if((int)s.size() != n){
return 0;
}
if(works){
return 1;
}
int a[n];
int val = inputSeq[idx];
for(int i = idx; i < n; i++){
a[i] = val++;
val %= n;
if(val == 0) val = n;
}
for(int i = 0; i < idx; i++){
a[i] = val++;
val %= n;
if(val == 0) val = n;
}
bool works2 = true;
for(int i = 0; i < n; i++){
if(inputSeq[i] <= n){
if(inputSeq[i] != a[i]){
works2 = false;
}
}
}
if(works2){
return 1;
}
return 0;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]){
//3, 8, 5, 6, 2
int maxVal = 0;
int minVal = INT_MAX;
for(int x = 0; x < n; x++){
maxVal = max(maxVal, gondolaSeq[x]);
minVal = min(minVal, gondolaSeq[x]);
}
if(maxVal <= n){
return 0;
}
if(minVal > n){
int x[maxVal + 1];
for(int a = n + 1; a <= maxVal; a++){
x[a] = 0;
}
int idx = 0;
for(int i = 0; i < n; i++){
if(gondolaSeq[i] <= n){
idx = i;
}
}
int a[n];
for(int i = 0; i < n; i++){
a[i] = i + 1;
}
for(int i = 0; i < n; i++){
if(gondolaSeq[i] > n){
x[gondolaSeq[i]] = a[i];
}
}
for(int i = maxVal; i >= n + 1; i--){
if(x[i] == 0){
x[i] = x[i + 1];
}
}
map<int, vector<int> > mp;
for(int i = n + 1; i <= maxVal; i++){
mp[x[i]].push_back(i);
}
int val9 = 0;
int idx9 = 0;
vector<pair<int, vector<int> > > v9;
for(auto it : mp){
val9 += (int)it.second.size();
v9.push_back(make_pair(it.second[0], (it.second)));
}
sort(v9.begin(), v9.end());
//6, 7, 8, 9
for(pair<int, vector<int> > c : v9){
replacementSeq[idx9++] = x[c.first];
for(int a : c.second){
if(a == c.second.back()){
break;
}
replacementSeq[idx9++] = a;
}
}
return val9;
}
int x[maxVal + 1];
for(int a = n + 1; a <= maxVal; a++){
x[a] = 0;
}
int idx = 0;
for(int i = 0; i < n; i++){
if(gondolaSeq[i] <= n){
idx = i;
}
}
int a[n];
int val = gondolaSeq[idx];
for(int i = idx; i < n; i++){
a[i] = val++;
val %= n;
if(val == 0) val = n;
}
for(int i = 0; i < idx; i++){
a[i] = val++;
val %= n;
if(val == 0) val = n;
}
for(int i = 0; i < n; i++){
if(gondolaSeq[i] > n){
x[gondolaSeq[i]] = a[i];
}
}
for(int i = maxVal; i >= n + 1; i--){
if(x[i] == 0){
x[i] = x[i + 1];
}
}
map<int, vector<int> > mp;
for(int i = n + 1; i <= maxVal; i++){
mp[x[i]].push_back(i);
}
int val9 = 0;
int idx9 = 0;
vector<pair<int, vector<int> > > v9;
for(auto it : mp){
val9 += (int)it.second.size();
v9.push_back(make_pair(it.second[0], (it.second)));
}
sort(v9.begin(), v9.end());
//6, 7, 8, 9
for(pair<int, vector<int> > c : v9){
replacementSeq[idx9++] = x[c.first];
for(int a : c.second){
if(a == c.second.back()){
break;
}
replacementSeq[idx9++] = a;
}
}
return val9;
}
//----------------------
const long long MOD = 1000000009;
long long binaryExponention(long long a, long long b){
long long ans = 1;
while(b > 0){
if(b & 1){
ans = (ans * a) % MOD;
}
a = (a * a) % MOD;
b /= 2LL;
}
return ans;
}
int countReplacement(int n, int inputSeq[]){
if(valid(n, inputSeq) == 0) return 0;
bool works = true;
vector<int> broke;
for(int i = 0; i < n; i++){
if(inputSeq[i] < n){
works = false;
}
else{
broke.push_back(inputSeq[i]);
}
}
sort(broke.begin(), broke.end());
reverse(broke.begin(), broke.end());
long long ans = 1;
for(int i = 0; i < (int)broke.size(); i++){
if(i != (int)broke.size() - 1){
long long val = broke[i] - broke[i + 1] - 1;
long long val2 = binaryExponention(i + 1, val);
ans = (ans * val2) % MOD;
}
else{
long long val = broke[i] - n;
long long val2 = binaryExponention((int)broke.size(), val);
ans = (ans * val2) % MOD;
}
}
if(works){
ans = (ans * (long long)n) % MOD;
}
return ans;
}
Compilation message
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:65:13: warning: variable 'idx' set but not used [-Wunused-but-set-variable]
65 | int idx = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
2484 KB |
Output is correct |
7 |
Correct |
20 ms |
4180 KB |
Output is correct |
8 |
Correct |
16 ms |
4724 KB |
Output is correct |
9 |
Correct |
5 ms |
1624 KB |
Output is correct |
10 |
Correct |
17 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
2808 KB |
Output is correct |
7 |
Correct |
19 ms |
4200 KB |
Output is correct |
8 |
Correct |
14 ms |
4700 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
10 |
Correct |
19 ms |
5468 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
9 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
29 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
1096 KB |
Output is correct |
12 |
Correct |
7 ms |
1116 KB |
Output is correct |
13 |
Correct |
28 ms |
6972 KB |
Output is correct |
14 |
Correct |
6 ms |
1116 KB |
Output is correct |
15 |
Correct |
27 ms |
6780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
600 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
440 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
29 ms |
4752 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
440 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
25 ms |
4916 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |