#include <bits/stdc++.h>
using namespace std;
int MOD = 1e9 + 7;
long long fast_power(long long base, long long power) {
long long result = 1;
while (power > 0) {
if (power % 2 == 1) {
result = (result * base) % MOD;
}
base = (base * base) % MOD;
power = power / 2;
}
return result;
}
int valid_1_2(int n, vector<int> input) {
for (int i = 0; i < n; i++) {
if (input[i] != 1) {
if (i) {
if (input[i] <= input[i - 1]) {
return 0;
}
} else {
if (input[i] <= input[n - 1]) {
return 0;
}
}
}
}
return 1;
}
int valid_3(int n, vector<int> input) {
bool first = false;
int fx=0;
for (int i = 0; i < n; i++) {
int x = input[i];
if (x > n) {
fx++;
if (i) {
if (input[i-1] == n) {
input[i]=1;
} else {
input[i]=input[i-1]+1;
}
} else {
first = true;
}
}
}
if(fx==n){
return 1;
}
if (first) {
if (input[1] == 1) {
input[0] = n;
} else {
input[0] = input[1] - 1;
}
}
for (int i = 0; i < n; i++) {
if (input[i] != 1) {
if (i) {
if (input[i] != input[i - 1]+1) {
return 0;
}
} else {
if (input[i] != input[n - 1]+1) {
return 0;
}
}
}
}
return 1;
}
int replacement(int n, vector<int> gondola, vector<int> rep) {
int ar[250005];
memset(ar, 0, sizeof ar);
int mx = 0;
for (int i = 0; i < n; i++) {
int x = gondola[i];
mx = max(mx, x);
ar[x] = 1;
}
for (int i = 1; i <= mx; i++) {
if (ar[i] == 0) {
rep.push_back(i);
}
}
return rep.size();
}
int countReplacement(int n, vector<int> input) {
int mx = 0;
long long ret = 1;
vector<int> flex;
flex.push_back(-1);
int fx=0;
for (int i = 0; i < n; i++) {
int x = input[i];
mx = max(mx, x);
if (x > n) {
fx++;
flex.push_back(x - n);
}
}
int val = valid_3(n, input);
if (val) {
if (mx == n) {
return 1;
}
} else {
return 0;
}
if(fx==n){
return fx;
}
int mul = flex.size() - 1;
sort(flex.begin(), flex.end());
for (int i = 1; i < flex.size(); i++) {
int x = flex[i - 1] + 1;
ret *= fast_power(mul, ((flex[i] - x) - 1)) ;
ret %= MOD;
}
return ret % MOD;
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k;
cin >> k;
while (k--) {
int t;
cin >> t;
int n;
cin >> n;
vector<int> vi, vt;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
vi.push_back(x);
}
if (t == 1 || t == 2) {
cout << valid_1_2(n, vi) << endl;
} else if (t == 3) {
cout << valid_3(n, vi) << endl;
} else if (t == 4 || t == 5 || t == 6) {
cout << replacement(n, vi, vt) << endl;
} else {
cout << countReplacement(n, vi) << endl;
}
}
return 0;
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, std::vector<int>)':
gondola.cpp:123:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < flex.size(); i++) {
~~^~~~~~~~~~~~~
/tmp/ccMOLicW.o: In function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccLzwf4i.o:gondola.cpp:(.text.startup+0x0): first defined here
/tmp/ccMOLicW.o: In function `main':
grader.cpp:(.text.startup+0xc3): undefined reference to `countReplacement'
grader.cpp:(.text.startup+0xe2): undefined reference to `valid'
grader.cpp:(.text.startup+0x106): undefined reference to `replacement'
collect2: error: ld returned 1 exit status