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 <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] + 1) % n;
} 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 = 1e9 + 9ll;
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;
}
return ans;
}
# | 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... |