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 <bits/stdc++.h>
#include "gondola.h"
#define pb emplace_back
#define fi first
#define se second
using namespace std;
const int N = int(1e6) + 2;
const int mod = int(1e9) + 9;
int dem[N];
int valid(int n, int p[])
{
int mn = 0, res = -1;
for(int i = 0; i < n; ++i) {
if(p[mn] > p[i]) mn = i;
if(++dem[p[i]] > 1) res = 0;
}
if(p[mn] > n && res == -1) res = 1;
for(int i = mn, lim = mn + n; i < lim; ++i) {
--dem[p[i % n]];
if(p[i % n] > n || res != -1) continue;
if(p[i % n] - p[mn] != i - mn) res = 0;
}
if(res == -1) res = 1;
return res;
}
int replacement(int n, int p[], int res[])
{
int mx = n, cnt = 0;
for(int i = 0; i < n; ++i) {
mx = max(mx, p[i]);
++dem[p[i]];
}
for(int i = 1; i <= mx; ++i) {
if(!dem[i]) res[cnt ++] = i;
else --dem[i];
}
return cnt;
}
int countReplacement(int n, int p[])
{
if(!valid(n, p)) return 0;
int mx = 0, mn = n + 1, cnt = 0, res = 1;
for(int i = 0; i < n; ++i) {
mx = max(p[i], mx);
mn = min(mn, p[i]);
if(p[i] > n) ++cnt, dem[p[i]] = 1;
}
if(mn <= n) {
for(int i = n + 1; i <= mx; ++i) {
if(dem[i]) --cnt, --dem[i];
else res = 1ll * res * cnt % mod;
}
} else {
for(int i = n + 1; i <= mx; ++i) {
res = 1ll * res * cnt % mod;
if(dem[i]) --cnt, --dem[i];
}
}
return res;
}
//int n, a[N], cmd, res[N];
//
//int main()
//{
// ios_base::sync_with_stdio(0);
// cin.tie(0), cout.tie(0);
// if(fopen("test.inp", "r")) {
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
// }
// int nTest;
// cin >> nTest;
// while(nTest --) {
// cin >> cmd >> n;
// for(int i = 0; i < n; ++i) cin >> a[i];
// if(cmd == 1) cout << valid(n, a) << '\n';
// else if(cmd == 2) {
// int sz = replacement(n, a, res);
// for(int i = 0; i < sz; ++i) cout << res[i] << ' ';
// cout << '\n';
// } else cout << countReplacement(n, a) << '\n';
// }
//}
# | 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... |