# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
967161 | VMaksimoski008 | Gondola (IOI14_gondola) | C++14 | 25 ms | 4860 KiB |
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;
using ll = long long;
const int mod = 1e9 + 9;
int valid(int n, int inputSeq[]) {
int ans = 1;
int cnt = 0;
set<int> s;
int big = 0;
for(int i=0; i<n; i++) {
if(inputSeq[i] > n) big++;
}
vector<int> v(n);
int pos = -1;
for(int i=0; i<n; i++) {
if(inputSeq[i] <= n) {
pos = i;
v[i] = inputSeq[i];
break;
}
}
for(int i=pos+1; i<n; i++) {
v[i] = v[i-1] + 1;
//if(v[i] > n) v[i] -= n;
}
for(int i=pos-1; i>=0; i--) {
v[i] = v[i+1] - 1;
//if(v[i] <= 0)
}
for(int i=0; i<n; i++) {
if(v[i] > n) v[i] -= n;
else if(v[i] <= 0) v[i] += n;
}
for(int i=0; i<n; i++) {
if(inputSeq[i] > n) continue;
if(inputSeq[i] != v[i]) return 0;
}
for(int i=0; i<n; i++) s.insert(inputSeq[i]);
if(s.size() < n) return 0;
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
int cnt = 0, big = 0, arr[n];
for(int i=0; i<n; i++) arr[i] = gondolaSeq[i];
for(int i=0; i<n; i++) big += (arr[i] > n);
if(big == n) {
vector<pair<int, int> > v;
for(int i=0; i<n; i++) v.push_back({ arr[i], i + 1 });
sort(v.begin(), v.end());
int curr = n + 1;
for(int i=0; i<n; i++) {
int a = v[i].second, b = v[i].first;
replacementSeq[cnt++] = a;
while(curr != b) {
replacementSeq[cnt++] = curr++;
}
curr++;
}
} else {
vector<int> v(n);
int pos = -1;
for(int i=0; i<n; i++) {
if(arr[i] <= n) {
pos = i;
v[i] = arr[i];
break;
}
}
for(int i=pos+1; i<n; i++) v[i] = v[i-1] + 1;
for(int i=pos-1; i>=0; i--) v[i] = v[i+1] - 1;
for(int i=0; i<n; i++) {
if(v[i] > n) v[i] -= n;
else if(v[i] <= 0) v[i] += n;
}
vector<pair<int, int> > ord;
for(int i=0; i<n; i++)
if(arr[i] > n) ord.push_back({ arr[i], v[i] });
sort(ord.begin(), ord.end());
int curr = n + 1;
for(auto &[b, a] : ord) {
replacementSeq[cnt++] = a;
while(curr != b) replacementSeq[cnt++] = curr++;
curr++;
}
}
return cnt;
}
ll exp(ll a, ll b) {
ll ans = 1;
while(b) {
if(b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return ans;
}
int countReplacement(int n, int inputSeq[]) {
if(!valid(n, inputSeq)) return 0;
ll ans = 1;
vector<int> v;
for(int i=0; i<n; i++)
if(inputSeq[i] > n) v.push_back(inputSeq[i]);
if(v.size() == 0) return 1;
sort(v.begin(), v.end());
int mx = v.back();
int sz = v.size();
ll curr = n + 1;
for(int i=0; i<sz; i++) {
if(v[i] == curr) {
curr++;
continue;
}
ans = (ans * exp(sz - i, v[i] - curr)) % mod;
curr = v[i] + 1;
}
if(sz == n) {
ll fac = 1;
for(int i=2; i<=n; i++) fac = (fac * i) % mod;
ans = (ans * fac) % mod;
}
return (int)ans;
}
Compilation message (stderr)
# | 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... |