Submission #872764

# Submission time Handle Problem Language Result Execution time Memory
872764 2023-11-13T17:23:50 Z HuyQuang_re_Zero Gondola (IOI14_gondola) C++14
100 / 100
47 ms 8020 KB
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define II pair <ll,ll>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define TII pair <treap*,treap*>
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
#define Log(x) (31-__builtin_clz((int)x))
#define LogLL(x) (63-__builtin_clzll((ll)x))
#include "gondola.h"
using namespace std;
int n,i,a[250005];
int valid(int n,int a[])
{
    int last_u=0,last_i=0;
    map <int,int> d;
    for(i=0;i<n;i++)
    {
        int u=a[i];
        if(d[u]) return 0;
        d[u]=1;
        if(u>n) continue;
        if(last_u>0 && (u-last_u+n)%n!=(i-last_i+n)%n)
            return 0;
        last_u=u; last_i=i;
    }
    return 1;
}
int m,res[250005],pos[250005],last[250005];
int replacement(int n,int a[], int res[])
{
    int ma=0,m=0;
    memset(last,-1,sizeof(last));
    for(i=0;i<n;i++) ma=max(ma,a[i]),last[a[i]]=i;
    for(i=1;i<=n;i++)
        if(last[i]>-1) break;
    if(i>n)
    {
        for(i=0;i<n;i++) a[i]=i+1;
    }
    else
    {
      //  cerr<<i<<" "<<last[i]<<'\n';
        int u=i,pos=last[i];
        a[pos]=i;
        for(i=pos+1;i<n;i++)
        {
            if(u==n) u=1; else u++;
            a[i]=u;
        }
        u=a[pos];
        for(i=pos-1;i>=0;i--)
        {
            if(u==1) u=n; else u--;
            a[i]=u;
        }
    }
    for(i=0;i<n;i++) pos[a[i]]=i;
    set <int> Replace;
    for(i=1;i<=n;i++)
        if(last[i]==-1) Replace.insert(i);
    for(i=n+1;i<=ma;i++)
        if(last[i]==-1)
        {
            int u=*Replace.begin(); Replace.erase(u);
            a[pos[u]]=i; pos[i]=pos[u];
            Replace.insert(i);
            res[m++]=u;
        }
        else
        {
            int u=a[last[i]]; Replace.erase(u);
            a[pos[u]]=i; pos[i]=pos[u];
            res[m++]=u;
        }
    return m;
}

const ll mod=round(1e9)+9;
ll lt(ll a,ll b)
{
    if(b==0) return 1;
    ll tg=lt(a,b/2);
    return (b%2==0) ? tg*tg%mod : tg*tg%mod*a%mod;
}
int countReplacement(int n,int a[])
{
    if(!valid(n,a)) return 0;
    vector <int> val;
    for(i=0;i<n;i++)
        if(a[i]>n) val.push_back(a[i]);
    int last=n,left=val.size();
    ll res=1;
    if(left==n) res=n;
    sort(val.begin(),val.end());
    for(int x:val)
    {
        res=res*lt(left,x-last-1)%mod;
        last=x; left--;
    }
    return res;
}

/*
int main()
{
    freopen("gondola.inp","r",stdin);
    freopen("gondola.out","w",stdout);
    cin>>n;
    for(i=0;i<n;i++) cin>>a[i];
    cout<<countReplacement(n,a);
}
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 10 ms 4696 KB Output is correct
7 Correct 7 ms 3284 KB Output is correct
8 Correct 19 ms 6468 KB Output is correct
9 Correct 6 ms 3676 KB Output is correct
10 Correct 22 ms 7016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 10 ms 4420 KB Output is correct
7 Correct 6 ms 3168 KB Output is correct
8 Correct 19 ms 6380 KB Output is correct
9 Correct 6 ms 3676 KB Output is correct
10 Correct 23 ms 7092 KB Output is correct
11 Correct 1 ms 2400 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 0 ms 2396 KB Output is correct
15 Correct 6 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4712 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4696 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4696 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4728 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4540 KB Output is correct
11 Correct 7 ms 5468 KB Output is correct
12 Correct 7 ms 5468 KB Output is correct
13 Correct 24 ms 6748 KB Output is correct
14 Correct 6 ms 5468 KB Output is correct
15 Correct 37 ms 6820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2640 KB Output is correct
5 Correct 1 ms 2492 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 33 ms 6480 KB Output is correct
10 Correct 25 ms 5788 KB Output is correct
11 Correct 10 ms 3676 KB Output is correct
12 Correct 13 ms 3932 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 33 ms 6524 KB Output is correct
10 Correct 25 ms 5980 KB Output is correct
11 Correct 10 ms 3676 KB Output is correct
12 Correct 13 ms 4120 KB Output is correct
13 Correct 3 ms 2652 KB Output is correct
14 Correct 41 ms 7492 KB Output is correct
15 Correct 47 ms 8020 KB Output is correct
16 Correct 8 ms 3420 KB Output is correct
17 Correct 31 ms 6340 KB Output is correct
18 Correct 16 ms 4444 KB Output is correct