#include "gondola.h"
#include<stack>
#include<utility>
#include<queue>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
int m2[100005];
bool exist[250005];
int res[250005], origin[100005];
const ll MOD=1e9+9;
int valid(int N, int *M)
{
int val=M[0], idx=0;
for(int i=0 ; i<N ; i++) m2[i]=M[i];
sort(m2,m2+N);
for(int i=1 ; i<N ; i++)
{
if(m2[i]==m2[i-1]) return 0;
}
for(int i=1 ; i<N ; i++)
{
if(val>M[i]) val=M[i], idx=i;
}
if(val>N) return 1;
int nxt=val;
for(int i=1 ; i<N ; i++)
{
(idx+=1)%=N;
nxt++;
if(nxt>N) nxt=1;
if(M[idx]>N) continue;
if(M[idx]!=nxt) return 0;
}
return 1;
}
void make_origin(int N, int *M)
{
int val=M[0], idx=0;
for(int i=1 ; i<N ; i++) if(val>M[i]) val=M[i], idx=i;
if(val>N)
{
for(int i=0 ; i<N ; i++) origin[i]=i+1;
return;
}
int nxt=val;
origin[idx]=nxt;
for(int i=1 ; i<N ; i++)
{
(idx+=1)%=N;
nxt++;
if(nxt>N) nxt=1;
origin[idx]=nxt;
}
}
int replacement(int N, int *M, int *ret)
{
make_origin(N,M);
priority_queue<P> PQ;
for(int i=0 ; i<N ; i++)
{
exist[M[i]]=true;
PQ.push(P(M[i],i));
}
stack<int> S;
int idx=PQ.top().first;
while(PQ.top().first!=N)
{
int pval, tsec=PQ.top().second;
while(exist[idx]) idx--;
pval=idx;
if(idx<=N) pval=origin[tsec];
PQ.pop();
PQ.push(P(pval,tsec));
exist[pval]=true;
S.push(pval);
}
int rcnt=0;
while(!S.empty())
{
ret[rcnt++]=S.top();
S.pop();
}
return rcnt;
}
ll get_pow(ll x, int y)
{
if(y==0) return 1;
if(y==1) return x;
ll ret=get_pow(x,y/2);
(ret*=ret)%=MOD;
if(y%2==1) (ret*=x)%=MOD;
return ret;
}
int countReplacement(int N, int *M)
{
int val=M[0], idx=0;
ll ret=0, ecnt=0;
if(!valid(N,M)) return (int)ret;
for(int i=1 ; i<N ; i++) if(val>M[i]) val=M[i], idx=i;
priority_queue<int,vector<int>,greater<int> > PQ;
if(val>N)
{
ret=N, ecnt=N;
for(int i=0 ; i<N ; i++) PQ.push(M[i]);
}
else
{
ret=1;
int nxt=val;
for(int i=1 ; i<N ; i++)
{
(idx+=1)%=N;
nxt++;
if(nxt>N) nxt=1;
if(nxt!=M[idx]) PQ.push(M[idx]), ecnt++;
}
}
val=N+1;
while(!PQ.empty())
{
int y=PQ.top()-val;
(ret*=get_pow(ecnt,y))%=MOD;
ecnt--;
val=PQ.top()+1;
PQ.pop();
}
return (int)ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
9 ms |
640 KB |
Output is correct |
7 |
Correct |
18 ms |
1040 KB |
Output is correct |
8 |
Correct |
14 ms |
896 KB |
Output is correct |
9 |
Correct |
7 ms |
576 KB |
Output is correct |
10 |
Correct |
25 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
9 ms |
640 KB |
Output is correct |
7 |
Correct |
18 ms |
1024 KB |
Output is correct |
8 |
Correct |
22 ms |
896 KB |
Output is correct |
9 |
Correct |
8 ms |
512 KB |
Output is correct |
10 |
Correct |
18 ms |
896 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
3 ms |
256 KB |
Output is correct |
13 |
Correct |
9 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
18 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
24 ms |
2160 KB |
Output is correct |
12 |
Correct |
27 ms |
2288 KB |
Output is correct |
13 |
Correct |
25 ms |
1768 KB |
Output is correct |
14 |
Correct |
15 ms |
2168 KB |
Output is correct |
15 |
Correct |
41 ms |
2484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
284 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
31 ms |
1280 KB |
Output is correct |
10 |
Correct |
21 ms |
1280 KB |
Output is correct |
11 |
Correct |
11 ms |
760 KB |
Output is correct |
12 |
Correct |
12 ms |
768 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
24 ms |
1280 KB |
Output is correct |
10 |
Correct |
19 ms |
1152 KB |
Output is correct |
11 |
Correct |
9 ms |
680 KB |
Output is correct |
12 |
Correct |
11 ms |
768 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
33 ms |
1660 KB |
Output is correct |
15 |
Correct |
42 ms |
1700 KB |
Output is correct |
16 |
Correct |
9 ms |
640 KB |
Output is correct |
17 |
Correct |
24 ms |
1280 KB |
Output is correct |
18 |
Correct |
15 ms |
1024 KB |
Output is correct |