| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1321643 | tsetsenbileg | Gondola (IOI14_gondola) | C++20 | 0 ms | 0 KiB |
#include "gondola.h"
#include <bits/stdc++.h>
// #define int long long
using ll = long long;
using namespace std;
#define pb push_back
using pr = pair<int, int>;
const int INF = 1e9+7, MOD = 1e9+9;
ll modpow(ll a, ll p) {
ll ans = 1;
while (p > 0) {
if (p & 1) ans = (ans * a) % MOD;
a = (a * a) % MOD;
p >>= 1;
}
return ans;
}
int startpos(int n, const vector<int>& a) {
int minpos = min_element(a.begin(), a.end()) - a.begin();
int start = ((minpos - a[minpos] + 1) % n + n) % n;
return start;
}
int valid(int n, int inputSeq[]) {
vector<int> a(n);
for (int i = 0; i < n; i++) a[i] = inputSeq[i];
int cur = 1;
int start = startpos(n, a);
for (int i = start; i < n; i++) {
if (a[i] > n || cur == a[i]) {
cur++;
}
else return 0;
}
for (int i = 0; i < start; i++) {
if (a[i] > n || cur == a[i]) {
cur++;
}
else return 0;
}
sort(a.begin(), a.end());
auto it = upper_bound(a.begin(), a.end(), n) - a.begin();
if (it == n) return 1;
if (a[it] != n+1) return 0;
for (int i = it+1; i < n; i++) {
if (a[i] != a[i-1] + 1) return 0;
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
vector<int> a(n);
for (int i = 0; i < n; i++) a[i] = (gondolaSeq[i]);
int start = startpos(n, a);
vector<pr> broke;
int cur = 1;
for (int i = start; i < n; i++) {
if (a[i] > n) {
broke.pb({a[i], cur});
}
cur++;
}
for (int i = 0; i < start; i++) {
if (a[i] > n) {
broke.pb({a[i], cur});
}
cur++;
}
int l = 0;
sort(broke.begin(), broke.end());
int curn = n+1;
for (auto [num, fir] : broke) {
replacementSeq[l] = fir;
l++;
for (int i = curn; i < num; i++) {
replacementSeq[l] = i;
l++;
}
curn = num+1;
}
return l;
}
int countReplacement(int n, int inputSeq[]){
vector<int> a(n);
for (int i = 0; i < n; i++) a[i] = (inputSeq[i]);
int start = startpos(n, a);
vector<pr> broke;
int cur = 1;
ll res = 1;
for (int i = start; i < n; i++) {
if (a[i] > n) {
broke.pb({a[i], cur});
}
else if (a[i] != cur) {
res = 0;
}
cur++;
}
for (int i = 0; i < start; i++) {
if (a[i] > n) {
broke.pb({a[i], cur});
}
else if (a[i] != cur) {
res = 0;
}
cur++;
}
sort(broke.begin(), broke.end());
if (cnt.size() == n) res = (res * n) % MOD;
ll curn = n, cnt = broke.size();
for (auto [num, fir] : broke) {
res = (res * modpow(cnt, (num-1-curn))) % MOD;
curn = num;
cnt--;
}
return res;
}
