Submission #1005826

# Submission time Handle Problem Language Result Execution time Memory
1005826 2024-06-23T06:35:57 Z Gray Gondola (IOI14_gondola) C++17
100 / 100
47 ms 8272 KB
#include<bits/stdc++.h>
#include "gondola.h"
#define ll long long
#define ff first
#define ss second
#define ln "\n"

using namespace std;
const ll MOD = 1e9+9;

int valid(int n, int inputSeq[])
{
  vector<ll> sn(n);
  ll trunk=-1;
  map<ll, ll> usd;
  for (ll i=0; i<n; i++){
    if (usd[inputSeq[i]]) return 0;
    usd[inputSeq[i]]=1;
    if (inputSeq[i]<=n) {
      sn[i]=inputSeq[i];
      trunk=i;
    }
  }
  if (trunk==-1) return 1;
  ll init = inputSeq[trunk]-1;
  for (ll i=trunk-1; i>=0; i--){
    sn[i]=(--init+n)%n+1;
  }
  init=inputSeq[trunk]-1;
  for (ll i=trunk+1; i<n; i++){
    sn[i]=(++init)%n+1;
  }
  for (ll i=0; i<n; i++){
    if (inputSeq[i]<=n and inputSeq[i]!=sn[i]) return 0;
  }
  return 1;
}

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

int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
  vector<ll> sn(n);
  ll trunk=-1;
  for (ll i=0; i<n; i++){
    if (gondolaSeq[i]<=n) {
      sn[i]=gondolaSeq[i];
      trunk=i;
    }
  }
  ll init = gondolaSeq[trunk]-1;
  for (ll i=trunk-1; i>=0; i--){
    sn[i]=(--init+n)%n+1;
  }
  init=gondolaSeq[trunk]-1;
  for (ll i=trunk+1; i<n; i++){
    sn[i]=(++init)%n+1;
  }
  vector<pair<ll, ll>> rep;
  for (ll i=0; i<n; i++){
    if (gondolaSeq[i]>n){
      rep.push_back({gondolaSeq[i], sn[i]});
    }
  }
  sort(rep.begin(), rep.end());
  ll len=0, prev=n, last;
  for (ll i=0; i<(ll)rep.size(); i++){
    // cout << rep[i].ff << " " << rep[i].ss << ln;
    last=rep[i].ss;
    while (prev<rep[i].ff){
      replacementSeq[len]=last;
      len++;
      last=++prev;
    } 
  }
  return len;
}

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

ll binpow(ll a, ll b){
  if (b==0) return 1;
  ll res=binpow(a, b/2);
  res=res*res%MOD;
  if (b&1) res=res*a%MOD;
  return res;
}

int countReplacement(int n, int inputSeq[])
{
  if (!valid(n, inputSeq)) return 0;
  vector<ll> sn(n);
  ll trunk=-1;
  for (ll i=0; i<n; i++){
    if (inputSeq[i]<=n) {
      sn[i]=inputSeq[i];
      trunk=i;
    }
  }
  if (trunk==-1){
    for (ll i=0; i<n; i++){
      sn[i]=i+1;
    }
  }else{
    ll init = inputSeq[trunk]-1;
    for (ll i=trunk-1; i>=0; i--){
      sn[i]=(--init+n)%n+1;
    }
    init=inputSeq[trunk]-1;
    for (ll i=trunk+1; i<n; i++){
      sn[i]=(++init)%n+1;
    }  
  }
  
  vector<pair<ll, ll>> rep;
  for (ll i=0; i<n; i++){
    if (inputSeq[i]>n){
      rep.push_back({inputSeq[i], sn[i]});
    }
  }
  sort(rep.begin(), rep.end());
  ll prev=n;
  ll ans=1;
  for (ll i=0; i<(ll)rep.size(); i++){
    ans=ans*binpow(rep.size()-i, rep[i].ff-prev-1)%MOD;
    // cout << "H: " << rep.size()-i << "^" <<  rep[i].ff-prev-1 << "->" << binpow(rep.size()-i, rep[i].ff-prev-1) << ln;
    prev=rep[i].ff;
  }
  if (trunk==-1){
    ans=ans*n%MOD;
  }
  return ans;
}
# 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
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 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 12 ms 3164 KB Output is correct
7 Correct 6 ms 1372 KB Output is correct
8 Correct 23 ms 5724 KB Output is correct
9 Correct 6 ms 2140 KB Output is correct
10 Correct 30 ms 6480 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 344 KB Output is correct
6 Correct 11 ms 3160 KB Output is correct
7 Correct 6 ms 1480 KB Output is correct
8 Correct 25 ms 5716 KB Output is correct
9 Correct 7 ms 2136 KB Output is correct
10 Correct 31 ms 6484 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 18 ms 2836 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 33 ms 6628 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
# 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
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 1 ms 360 KB Output is correct
10 Correct 1 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 344 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 5 ms 1376 KB Output is correct
12 Correct 6 ms 1372 KB Output is correct
13 Correct 9 ms 1500 KB Output is correct
14 Correct 5 ms 1124 KB Output is correct
15 Correct 12 ms 2372 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
# 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
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 0 ms 348 KB Output is correct
9 Correct 38 ms 5776 KB Output is correct
10 Correct 31 ms 4944 KB Output is correct
11 Correct 10 ms 2064 KB Output is correct
12 Correct 11 ms 2692 KB Output is correct
13 Correct 3 ms 860 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
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
9 Correct 35 ms 5664 KB Output is correct
10 Correct 30 ms 4956 KB Output is correct
11 Correct 10 ms 2064 KB Output is correct
12 Correct 12 ms 2692 KB Output is correct
13 Correct 2 ms 860 KB Output is correct
14 Correct 42 ms 8012 KB Output is correct
15 Correct 47 ms 8272 KB Output is correct
16 Correct 7 ms 2000 KB Output is correct
17 Correct 34 ms 5624 KB Output is correct
18 Correct 18 ms 3644 KB Output is correct