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 DIM 100010
#define MOD 1000000009
using namespace std;
map <int,int> f;
pair <int,int> w[DIM];
int sol[DIM],a[DIM];
int get_next (int val, int n){
if (val == n)
return 1;
return val+1;
}
int get_ant (int val, int n){
if (val == 1)
return n;
return val-1;
}
inline int cmp (pair<int,int> a, pair<int,int> b){
return a.second < b.second;
}
int lg_put (int a, int b){
if (!b)
return 1;
int nr = lg_put (a,b/2);
if (b%2)
return 1LL*(1LL * nr * nr % MOD) * a % MOD;
else return (1LL * nr * nr % MOD);
}
int valid (int n, int v[]){
int i, poz = -1;
for (i=0;i<n;i++){
f[v[i]]++;
if (f[v[i]] >= 2)
return 0;
}
for (i=0;i<n;i++)
if (v[i] <= n){
poz = i;
break;
}
if (poz == -1)
return 1;
int val = v[poz];
for (i=poz+1;i<n;i++){
val = get_next (val,n);
if (v[i] <= n && v[i] != val)
return 0;
}
val = v[poz];
for (i=poz-1;i>=0;i--){
val = get_ant (val,n);
if (v[i] <= n && v[i] != val)
return 0;
}
return 1;
}
int replacement (int n, int v[], int replacementSeq[]){
int i, poz = -1, k = 0;
for (i=0;i<n;i++)
if (v[i] <= n){
poz = i;
break;
}
/// if (poz == -1) ???
int val = v[poz];
for (i=poz+1;i<n;i++){
val = get_next (val,n);
if (v[i] > n)
w[++k] = make_pair(val,v[i]);
}
val = v[poz];
for (i=poz-1;i>=0;i--){
val = get_ant (val,n);
if (v[i] > n)
w[++k] = make_pair(val,v[i]);
}
sort (w+1,w+k+1,cmp);
int last = n+1, el = 0;
for (i=1;i<=k;i++){
replacementSeq[el++] = w[i].first;
while (last <= w[i].second){
if (last < w[i].second)
replacementSeq[el++] = last;
last++;
}
}
return el;
}
int countReplacement(int n, int v[]){
if (!valid(n,v))
return 0;
int i, k = 0;
for (i=0;i<n;i++){
if (v[i] > n)
a[++k] = v[i];
}
sort (a+1,a+k+1);
a[0] = n;
long long sol = 1;
for (i=1;i<=k;i++)
sol = sol * lg_put (k-i+1,a[i]-a[i-1]-1) % MOD;
if (k == n)
sol = sol * n % MOD;
return sol;
}
# | 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... |