이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "gondola.h"
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const long long int MOD = 1e9 + 9;
int correct[MAXN], pos[3 * MAXN];
bool marc[3 * MAXN], all = false;
void calc(int n, int seq[])
{
int id = -1;
for(int i = 0; i < n; i++)
if(seq[i] <= n)
{ id = i; break; }
if(id != -1) correct[id] = seq[id];
else id = 0, correct[id] = 1, all = true;
for(int i = id + 1; i < n; i++)
{
correct[i] = correct[i - 1] + 1;
if(correct[i] > n) correct[i] = 1;
}
for(int i = id - 1; i >= 0; i--)
{
correct[i] = correct[i + 1] - 1;
if(correct[i] < 1) correct[i] = n;
}
}
int valid(int n, int seq[])
{
calc(n, seq);
for(int i = 0; i < n; i++)
{
if(seq[i] <= n && correct[i] != seq[i]) return 0;
if(seq[i] > n && marc[seq[i]]) return 0;
marc[seq[i]] = true;
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
calc(n, gondolaSeq);
for(int i = 0; i < 3 * MAXN; i++) pos[i] = -1;
int mx = n;
for(int i = 0; i < n; i++)
if(gondolaSeq[i] > n)
pos[gondolaSeq[i]] = i, mx = max(mx, gondolaSeq[i]);
for(int i = mx - 1; i > n; i--)
if(pos[i] == -1)
pos[i] = pos[i + 1];
int l = 0;
for(int i = n + 1; i <= mx; i++)
{
replacementSeq[l] = correct[pos[i]];
correct[pos[i]] = i;
l++;
}
return l;
}
long long int fe(long long int num, int exp)
{
if(exp == 0) return 1;
if(exp == 1) return num;
long long int aux = fe(num, exp / 2);
aux *= aux;
if(exp % 2) return aux % MOD;
return (aux * num) % MOD;
}
int countReplacement(int n, int inputSeq[])
{
if(!valid(n, inputSeq)) return 0;
vector<long long int> v;
for(int i = 0; i < n; i++)
if(inputSeq[i] > n)
v.push_back(inputSeq[i]);
sort(v.begin(), v.end());
long long int ans = 1;
long long int sz = v.size(), last = n;
for(int i = 0; i < v.size(); i++)
{
// printf("num = %lld exp = %lld\n", sz, v[i] - 1- last);
ans *= fe(sz, v[i] - 1 - last);
ans %= MOD;
last = v[i];
sz--;
}
if(all) ans *= n, ans %= MOD;
return ans;
}
/*
int N, SEQ[MAXN];
int main ()
{
scanf("%d", &N);
for(int i = 0; i < N; i++) scanf("%d", &SEQ[i]);
// printf("valid = %d\n", valid(N, SEQ));
printf("cont = %lld\n", countReplacement(N, SEQ));
}
*/
컴파일 시 표준 에러 (stderr) 메시지
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:99:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++)
~~^~~~~~~~~~
# | 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... |