이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
/**********************/
#define ll long long
const int mod = 1e9 + 9;
int valid(int n, int inputSeq[])
{
set<int> vis;
for(int i=0; i<n; i++){
if(vis.find(inputSeq[i])!= vis.end()) {
return false;
}
vis.insert(inputSeq[i]);
}
int preval = -1;
for(int i =n -1; i>=0; i--){
if(inputSeq[i] <=n){
preval = (n - i) + inputSeq[i];
if(preval> n) preval-=n;
break;
}
}
if(preval == -1) return true;
preval -=1;
for(int i =0; i<n; i++){
//cout << preval<<endl;
if(inputSeq[i] <=n){
if(preval == n){
if(inputSeq[i] != 1) return false;
}
else
if(inputSeq[i]!= preval+1) return false;
preval = inputSeq[i];
}
else{
//replacement, num can be anything
if(preval == n)
preval = 1;
else preval++;
}
}
return true;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
//debug second subtask, something weird is wrong but i am close
int preval = -1;
for(int i =n -1; i>=0; i--){
if(gondolaSeq[i] <=n){
preval = (n - i) + gondolaSeq[i];
if(preval> n) preval-=n;
break;
}
}
set<pair<int,int>> replace;
if(preval == -1){
//all of them were replaced
for(int i = 0; i<n; i++){
replace.insert({gondolaSeq[i], i+1});
}
}
else{
preval-=1;
//cout << preval<<endl;
for(int i =0; i<n; i++){
if(gondolaSeq[i] <=n){
preval = gondolaSeq[i];
}
else{
//replacement, num can be anything
if(preval == n){
replace.insert({gondolaSeq[i], 1});
preval = 1;
}
else {
replace.insert({gondolaSeq[i], ++preval});
}
}
}
}
int cnt = 0;
int prev = n;
for(pair<int,int> a : replace){
//cout <<a.first<<", "<<a.second<<endl;
replacementSeq[cnt] = a.second;
cnt++;
for(int i = prev+1; i< a.first; i++){
replacementSeq[cnt] = i;
cnt++;
}
prev = a.first;
}
return cnt;
}
//----------------------
ll binexp(ll a, int b){
if(b == 0) return 1;
ll ans = binexp(a, b/2) % mod;
if(b%2)
return (ans * ans) % mod * a % mod;
else return ans * ans % mod;
}
int countReplacement(int n, int inputSeq[])
{
if(!valid(n, inputSeq)) return 0;
int preval = -1;
for(int i =n -1; i>=0; i--){
if(inputSeq[i] <=n){
preval = (n - i) + inputSeq[i];
if(preval> n) preval-=n;
break;
}
}
set<int> replace;
if(preval == -1){
//all of them were replaced
for(int i = 0; i<n; i++){
replace.insert(inputSeq[i]);
}
}
else{
preval-=1;
//cout << preval<<endl;
for(int i =0; i<n; i++){
if(inputSeq[i] <=n){
preval = inputSeq[i];
}
else{
//replacement, num can be anything
if(preval == n){
replace.insert(inputSeq[i]);
preval = 1;
}
else {
++preval;
replace.insert(inputSeq[i]);
}
}
}
}
ll ans = 1;
if(preval == -1) ans = n;
int prev = n+1;
int cnt = 0;
for( int a: replace){
ans *= binexp((replace.size() - cnt), (a - prev)) ;
ans%= mod;
cnt++;
prev = a+1;
//cout << a<<": "<<ans<<endl;
}
return ans;
}
/******************************/
# | 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... |