답안 #1005825

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1005825 2024-06-23T06:34:16 Z Gray 곤돌라 (IOI14_gondola) C++17
75 / 100
40 ms 6740 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){
    for (ll i=2; i<=n; i++){
      ans=ans*i%MOD;
    }
  }
  return ans;
}
# 결과 실행 시간 메모리 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 1 ms 504 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 11 ms 3164 KB Output is correct
7 Correct 6 ms 1372 KB Output is correct
8 Correct 22 ms 5652 KB Output is correct
9 Correct 6 ms 2140 KB Output is correct
10 Correct 27 ms 6608 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
6 Correct 11 ms 3164 KB Output is correct
7 Correct 6 ms 1372 KB Output is correct
8 Correct 22 ms 5696 KB Output is correct
9 Correct 6 ms 2140 KB Output is correct
10 Correct 28 ms 6516 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 19 ms 2864 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 35 ms 6740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 472 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 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 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 5 ms 1372 KB Output is correct
12 Correct 6 ms 1372 KB Output is correct
13 Correct 13 ms 1620 KB Output is correct
14 Correct 5 ms 1368 KB Output is correct
15 Correct 12 ms 2184 KB Output is correct
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 40 ms 5764 KB Output is correct
10 Correct 30 ms 4956 KB Output is correct
11 Correct 10 ms 2064 KB Output is correct
12 Correct 13 ms 2528 KB Output is correct
13 Incorrect 3 ms 860 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 1 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 40 ms 5704 KB Output is correct
10 Correct 27 ms 4984 KB Output is correct
11 Correct 11 ms 2012 KB Output is correct
12 Correct 13 ms 2692 KB Output is correct
13 Incorrect 3 ms 860 KB Output isn't correct
14 Halted 0 ms 0 KB -