# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
316967 | VROOM_VARUN | Gondola (IOI14_gondola) | C++14 | 0 ms | 0 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.
/*
ID: varunra2
LANG: C++
TASK: gondola
*/
#include <bits/stdc++.h>
// #include "gondola.h"
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000009
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int n;
bool good(int val) { return val >= 1 and val <= n; }
VI get(int *vals) {
VI ret;
for (int i = 0; i < n; i++) {
if (good(vals[i])) ret.PB(i);
}
return ret;
}
VI brokeget(int *vals) { VI ret; }
bool distinct(int *inputSeq) {
MPII mp;
for (int i = 0; i < n; i++) {
mp[inputSeq[i]]++;
if (mp[inputSeq[i]] >= 2) return false;
}
return true;
}
int valid(int N, int *inputSeq) {
n = N;
VI inds = get(inputSeq);
if (!distinct(inputSeq)) return 0;
if (sz(inds) == 0) return 1;
for (int i = inds[0], val = inputSeq[inds[0]], cnt = 0; cnt < n;
cnt++, val = val % n + 1, i = (i + 1) % n) {
if (!good(inputSeq[i])) continue;
if (inputSeq[i] != val) return 0;
}
return 1;
}
VII genVals(int n, int *gondolaSeq) {
VII vals;
for (int i = 0; i < n; i++) {
if (!good(gondolaSeq[i])) {
vals.PB(MP(gondolaSeq[i], i));
}
}
return vals;
}
int replacement(int N, int *gondolaSeq, int *replacementSeq) {
assert(valid(n, gondolaSeq));
n = N;
VII vals = genVals(n, gondolaSeq);
if (vals.empty()) return 0;
// debug(vals);
sort(all(vals));
int lst = n + 1;
int cnt = 0;
auto x = get(gondolaSeq);
VI label(n);
if (x.empty()) {
for (int i = 0; i < n; i++) label[i] = i + 1;
} else {
for (int i = x[0], val = gondolaSeq[x[0]], cnt = 0; cnt < n;
cnt++, val = val % n + 1, i = (i + 1) % n) {
label[i] = val;
}
}
// debug(label);
for (int i = 0; i < sz(vals); i++) {
while (label[vals[i].y] != vals[i].x) {
replacementSeq[cnt] = label[vals[i].y];
label[vals[i].y] = lst;
lst++;
cnt++;
}
}
return cnt;
}
ll binpow(ll a, ll b) {
if (b == 0) return 1ll;
if (b == 1) return a;
ll mid = binpow(a, b / 2);
ll mult = (mid * mid) % MOD;
if (b % 2) return (mult * a) % MOD;
return mult;
}
int countReplacement(int N, int *inputSeq) {
n = N;
if (!valid(n, inputSeq)) return 0;
ll ret = 1;
VII vals = genVals(n, inputSeq);
if (vals.empty()) return 1;
sort(all(vals));
// debug(vals);
int lst = n;
int cnt = sz(vals);
for (int i = 0; i < sz(vals); i++) {
int len = vals[i].x - lst - 1;
lst = vals[i].x;
ret *= binpow(cnt, len);
ret %= MOD;
}
if (cnt == n) {
ret *= (1ll * n);
ret %= MOD;
}
return ret;
}
void test1() {
int n;
cin >> n;
int gondolaSeq[n];
int replacementSeq[50];
for (int i = 0; i < n; i++) {
cin >> gondolaSeq[i];
}
int val = replacement(n, gondolaSeq, replacementSeq);
cout << val << '\n';
for (int i = 0; i < val; i++) {
cout << replacementSeq[i] << " ";
}
if (val) cout << '\n';
}
void test2() {
int n;
cin >> n;
int gondolaSeq[n];
for (int i = 0; i < n; i++) {
cin >> gondolaSeq[i];
}
cout << countReplacement(n, gondolaSeq) << '\n';
}
// int main() {
// #ifndef ONLINE_JUDGE
// freopen("gondola.in", "r", stdin);
// freopen("gondola.out", "w", stdout);
// #endif
// cin.sync_with_stdio(0);
// cin.tie(0);
// int type;
// cin >> type;
// if (type == 1) {
// test1();
// } else
// test2();
// return 0;
// }