This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
int valid(int n, int inputSeq[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
set<int> st;
for (int i = 0; i < n; i++) {
if (st.count(inputSeq[i])) return 0;
st.insert(inputSeq[i]);
}
int lowest = 1e9, pos = 1e9;
for (int i = 0; i < n; i++) {
if (lowest > inputSeq[i]) {
lowest = inputSeq[i], pos = i;
}
}
if (lowest > n) return 1;
for (int i = pos, cnt = lowest, len = 1; len <= n; i = (i + 1) % n, len++, cnt = cnt % n + 1) {
if (inputSeq[i] > n) continue;
if (cnt != inputSeq[i]) return 0;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
if (*max_element(gondolaSeq, gondolaSeq + n) == n) return 0;
int lowest = 1e9, pos = 1e9;
for (int i = 0; i < n; i++) {
if (gondolaSeq[i] < lowest) {
lowest = gondolaSeq[i];
pos = i;
}
}
vector<pair<int, int>> arr;
if (lowest > n) {
for (int i = 0; i < n; i++) arr.push_back({gondolaSeq[i], i + 1});
sort(arr.begin(), arr.end());
} else {
for (int i = pos, len = 1, cnt = lowest; len <= n; i = (i + 1) % n, len++, cnt = cnt % n + 1) {
if (gondolaSeq[i] <= n) continue;
arr.push_back({gondolaSeq[i], cnt});
}
sort(arr.begin(), arr.end());
}
int index = 0, cur = n + 1;
for (auto&x : arr) {
int v = x.first, pos = x.second;
replacementSeq[index++] = pos;
for (int i = cur; i < v; i++) replacementSeq[index++] = i;
cur = v + 1;
}
return index;
}
//----------------------
long long binpow(int n, int k) {
const long long modulo = 1e9 + 9;
if (!k) return 1;
else {
long long ans = binpow(n, k/2);
ans *= ans, ans %= modulo;
if (k & 1) ans *= (long long)n, ans %= modulo;
return ans;
}
}
int countReplacement(int n, int inputSeq[]) {
if (!valid(n, inputSeq)) return 0;
if (*max_element(inputSeq, inputSeq + n) == n) return 1;
vector<int> arr = {n};
for (int i = 0; i < n; i++) {
if (inputSeq[i] <= n) continue;
arr.push_back(inputSeq[i]);
}
sort(arr.begin(), arr.end());
const long long modulo = 1e9 + 9;
long long ans = 1;
for (int i = 1; i < arr.size(); i++) ans *= binpow(arr.size() - i, arr[i] - arr[i - 1] - 1), ans %= modulo;
if (*min_element(inputSeq, inputSeq + n) > n) {
for (int i = 1; i <= n; i++) {
ans *= (long long)i, ans %= modulo;
}
}
return (int)ans;
}
/*int gondolaSequence[100001];
int replacementSequence[250001];
signed main()
{
int i, n, tag;
int nr;
//assert(scanf("%d", &tag)==1);
//assert(scanf("%d", &n)==1);
cin >> tag >> n;
for(i=0;i< n;i++) cin >> gondolaSequence[i];
//assert( scanf("%d", &gondolaSequence[i]) ==1);
switch (tag){
case 1: case 2: case 3:
printf("%d\n", valid(n, gondolaSequence));
break;
case 4: case 5: case 6:
nr = replacement(n, gondolaSequence, replacementSequence);
printf("%d ", nr);
if (nr > 0)
{
for (i=0; i<nr-1; i++)
printf("%d ", replacementSequence[i]);
printf("%d\n", replacementSequence[nr-1]);
}
else printf("\n");
break;
case 7: case 8: case 9: case 10:
printf("%d\n", countReplacement(n, gondolaSequence));
break;
}
return 0;
}*/
Compilation message (stderr)
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 1; i < arr.size(); i++) ans *= binpow(arr.size() - i, arr[i] - arr[i - 1] - 1), ans %= modulo;
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |