# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
931830 | OAleksa | 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.
#include "gondola.h"
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
int valid(int n, int input[])
{
vector<int> a;
for (int i = 0;i < n;i++)
a.push_back(input[i]);
for (int i = 0;i < n;i++)
a.push_back(input[i]);
int mn = -1;
map<int, int> cnt;
for (int i = 0;i < n;i++) {
cnt[a[i]]++;
if (mn == -1 || a[i] < a[mn])
mn = i;
}
for (auto u : cnt) {
if (u.s > 1)
return 0;
}
for (int i = mn, j = 0;j < n;i++,j++) {
if (a[i] > n)
continue;
if (a[i] != a[mn] + j)
return 0;
}
return 1;
}
//----------------------
int replacement(int n, int g[], int ans[])
{
int sz = 0;
if (!valid(n, g))
return sz;
vector<int> a;
for (int i = 0;i < n;i++)
a.push_back(g[i]);
for (int i = 0;i < n;i++)
a.push_back(g[i]);
int mn = -1;
for (int i = 0;i < n;i++) {
if (a[i] <= n && (mn == -1 || a[i] < a[mn]))
mn = i;
}
vector<pair<int, int>> pos;
if (mn == -1) {
mn = 0;
for (int i = 0;i < n;i++) {
if (a[i] < a[mn])
mn = i;
}
for (int i = mn, j = 1;j <= n;i++, j++) {
pos.push_back({a[i], j});
}
}
else {
for (int i = mn, j = 0;j < n;i++, j++) {
if (a[i] > n) {
int x = a[mn] + j;
if (x > n)
x -= n;
pos.push_back({a[i], x});
}
}
}
sort(pos.begin(), pos.end());
int lst = n;
for (auto i : pos) {
int br = i.s, x = i.f;
while (x > lst) {
ans[sz++] = br;
br = ++lst;
}
}
return sz;
}
//----------------------
int countReplacement(int n, int g[])
{
const int mod = 1e9 + 9;
auto mul = [&](int a, int b) {
long long r = 1ll * a * b;
if (r >= mod)
r %= mod;
return r;
};
auto binpow = [&](int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
};
if (!valid(n, g))
return 0;
vector<int> p;
for (int i = 0;i < n;i++) {
if (g[i] > n)
p.push_back(g[i]);
}
sort(p.begin(), p.end());
int m = p.size();
int lst = n;
int ans = 1;
for (int i = 0;i < m;i++) {
int r = p[i] - lst;
ans = mul(ans, binpow(m - i, r - 1));
lst = p[i];
}
if (g.size() == n)
ans = mul(ans, n);
return ans;
}