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(3e5) + 2;
const int M = int(1e6) + 1;
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;
}
typedef pair<int, int> pii;
int replacement(int n, int p[], int res[])
{
int mn = 0, cnt = 0;
vector<int> org(n);
vector<pii> v;
for(int i = 0; i < n; ++i) {
if(p[mn] > p[i]) mn = i;
if(p[i] > n) v.pb(i, p[i]);
}
sort(v.begin(), v.end(), [](const pii& x, const pii& y){
return x.se < y.se;
});
for(int i = mn, lim = mn + n; i < lim; ++i) {
if(p[mn] > n) org[i - mn] = i - mn + 1;
else org[i % n] = (p[mn] + i - mn - 1) % n + 1;
}
mn = n + 1;
for(pii x: v) {
res[cnt ++] = org[x.fi];
while(mn < x.se) res[cnt ++] = mn ++;
mn = x.se + 1;
}
return cnt;
}
int Power(int x, int y)
{
int res = 1;
while(y) {
if(y & 1) res = 1ll * res * x % mod;
x = 1ll * x * x % mod;
y >>= 1;
}
return res;
}
int countReplacement(int n, int p[])
{
if(!valid(n, p)) return 0;
int mn = n + 1, pre, res;
vector<int> v;
for(int i = 0; i < n; ++i) {
mn = min(mn, p[i]);
if(p[i] > n) v.pb(p[i]);
}
sort(v.begin(), v.end(), greater<int>());
res = mn > n? n: 1;
pre = n + 1;
while(!v.empty()) {
res = 1ll * res * Power(v.size(), v.back() - pre) % mod;
pre = v.back() + 1; v.pop_back();
}
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... |