Submission #991842

# Submission time Handle Problem Language Result Execution time Memory
991842 2024-06-03T08:39:07 Z huutuan Gondola (IOI14_gondola) C++14
100 / 100
54 ms 5976 KB
#include "gondola.h"

#include <bits/stdc++.h>

using namespace std;

int valid(int n, int inputSeq[])
{
   set<int> st;
   for (int i=0; i<n; ++i) if (inputSeq[i]<=n && inputSeq[(i+1)%n]<=n){
      if (inputSeq[i]%n+1!=inputSeq[(i+1)%n]){
         return 0;
      }
   }
   for (int i=0; i<n; ++i) if (!st.insert(inputSeq[i]).second) return 0; 
   return 1;
}

//----------------------

int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
   int max_id=max_element(gondolaSeq, gondolaSeq+n)-gondolaSeq;
   int max_val=gondolaSeq[max_id];
   vector<int> pos(3e5, -1);
   vector<int> perm(n);
   iota(perm.begin(), perm.end(), 1);
   for (int i=0; i<n; ++i) pos[gondolaSeq[i]]=i;
   for (int i=0; i<n; ++i) if (gondolaSeq[i]<=n){
      perm[i]=gondolaSeq[i];
      for (int j=(i+1)%n, k=perm[i]%n+1; j!=i; j=(j+1)%n, k=k%n+1) perm[j]=k;
      break; 
   }
   int cnt=0;
   for (int i=n+1; i<=max_val; ++i){
      if (pos[i]!=-1){
         replacementSeq[cnt++]=perm[pos[i]];
         perm[pos[i]]=i;
      }else{
         replacementSeq[cnt++]=perm[max_id];
         perm[max_id]=i;
      }
   }
   return cnt;
}

//----------------------

const int mod=1e9+9;

int binpow(int x, int y){
   int t=1;
   while (y){
      if (y&1) t=1ll*t*x%mod;
      x=1ll*x*x%mod;
      y>>=1;
   }
   return t;
}

int countReplacement(int n, int inputSeq[])
{
   if (!valid(n, inputSeq)) return 0;
   int max_id=max_element(inputSeq, inputSeq+n)-inputSeq;
   int max_val=inputSeq[max_id];
   int ans=n;
   set<int> st(inputSeq, inputSeq+n);
   int cnt=n;
   for (int i=0; i<n; ++i) if (inputSeq[i]<=n){
      ans=1;
      --cnt;
   }
   int cur=n+1;
   for (int i:st){
      if (i<=n) continue;
      ans=1ll*ans*binpow(cnt, i-cur)%mod;
      --cnt;
      cur=i+1;
   }
   return ans;
}

Compilation message

gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:65:8: warning: unused variable 'max_val' [-Wunused-variable]
   65 |    int max_val=inputSeq[max_id];
      |        ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 8 ms 2140 KB Output is correct
7 Correct 5 ms 604 KB Output is correct
8 Correct 13 ms 3932 KB Output is correct
9 Correct 5 ms 1372 KB Output is correct
10 Correct 18 ms 4464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 396 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 8 ms 2140 KB Output is correct
7 Correct 5 ms 604 KB Output is correct
8 Correct 13 ms 4040 KB Output is correct
9 Correct 6 ms 1372 KB Output is correct
10 Correct 17 ms 4604 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 5 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1628 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 1 ms 1372 KB Output is correct
5 Correct 1 ms 1624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1628 KB Output is correct
2 Correct 1 ms 1628 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 1 ms 1372 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
6 Correct 1 ms 1624 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1628 KB Output is correct
9 Correct 1 ms 1628 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1624 KB Output is correct
2 Correct 1 ms 1368 KB Output is correct
3 Correct 1 ms 1372 KB Output is correct
4 Correct 1 ms 1372 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
6 Correct 1 ms 1372 KB Output is correct
7 Correct 2 ms 1628 KB Output is correct
8 Correct 1 ms 1628 KB Output is correct
9 Correct 1 ms 1624 KB Output is correct
10 Correct 1 ms 1628 KB Output is correct
11 Correct 6 ms 2140 KB Output is correct
12 Correct 7 ms 2100 KB Output is correct
13 Correct 8 ms 2140 KB Output is correct
14 Correct 6 ms 2008 KB Output is correct
15 Correct 12 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 39 ms 3932 KB Output is correct
10 Correct 30 ms 3548 KB Output is correct
11 Correct 11 ms 1372 KB Output is correct
12 Correct 13 ms 1624 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 38 ms 4164 KB Output is correct
10 Correct 29 ms 3420 KB Output is correct
11 Correct 10 ms 1372 KB Output is correct
12 Correct 12 ms 1828 KB Output is correct
13 Correct 3 ms 604 KB Output is correct
14 Correct 47 ms 5460 KB Output is correct
15 Correct 54 ms 5976 KB Output is correct
16 Correct 10 ms 1328 KB Output is correct
17 Correct 33 ms 4184 KB Output is correct
18 Correct 18 ms 2616 KB Output is correct