답안 #754681

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
754681 2023-06-08T10:55:08 Z marvinthang 곤돌라 (IOI14_gondola) C++17
100 / 100
19 ms 2044 KB
/*************************************
*    author: marvinthang             *
*    created: 08.06.2023 17:22:12    *
*************************************/

#include "gondola.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// end of template

void shift(int n, int seq[]) {
    REP(i, n) if (seq[i] <= n) {
        rotate(seq, seq + (i - seq[i] + 1 + n) % n, seq + n);
        return;
    }
}

int valid(int n, int inputSeq[]) {
    shift(n, inputSeq);
    REP(i, n) if (inputSeq[i] <= n && inputSeq[i] != i + 1) return 0;
    sort(inputSeq, inputSeq + n);
    REP(i, n - 1) if (inputSeq[i] == inputSeq[i + 1]) return 0;
    return 1;
}

int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
    shift(n, gondolaSeq);
    int ma = *max_element(gondolaSeq, gondolaSeq + n);
    vector <int> pos(ma + 1, -1);
    REP(i, n) pos[gondolaSeq[i]] = i;
    REPD(i, ma) if (pos[i] == -1) pos[i] = pos[i + 1];
    REP(i, n) gondolaSeq[i] = i + 1;
    int cnt = 0;
    FORE(i, n + 1, ma) {
        replacementSeq[i - n - 1] = gondolaSeq[pos[i]];
        gondolaSeq[pos[i]] = i;
    }
    return ma - n;
}

const int MOD = 1e9 + 9;

void mul(int &a, int b) {
    a = 1LL * a * b % MOD;
}

int POW(int a, int b) {
    int res = 1;
    for (; b; b >>= 1) {
        if (b & 1) mul(res, a);
        mul(a, a);
    }
    return res;
}

int countReplacement(int n, int inputSeq[]) {
    if (!valid(n, inputSeq)) return 0;
    int res = 1;
    REP(i, n) if (inputSeq[i] > n) mul(res, POW(n - i, inputSeq[i] - (i ? max(inputSeq[i - 1], n) : n) - 1));
    if (inputSeq[0] > n) mul(res, n);
    return res;
}

#ifdef LOCAL
#include <stdio.h>
#include <assert.h>
#include "gondola.h"

int gondolaSequence[100001];
int replacementSequence[250001];

int main()
{
    file("gondola");
  int i, n, tag;
  int nr; 
  assert(scanf("%d", &tag)==1);
  assert(scanf("%d", &n)==1);
  for(i=0;i< n;i++)
    assert( scanf("%d", &gondolaSequence[i]) ==1);
  
  switch (tag){
  case 1: case 2: case 3:
    printf("%d\n", valid(n, gondolaSequence));
    break;

  case 4: case 5: case 6:
    nr = replacement(n, gondolaSequence, replacementSequence);
    printf("%d ", nr);
    if (nr > 0)
      {
  for (i=0; i<nr-1; i++)
    printf("%d ", replacementSequence[i]);
  printf("%d\n", replacementSequence[nr-1]);
      }  
    else printf("\n");
    break;

  case 7: case 8: case 9: case 10:
    printf("%d\n",  countReplacement(n, gondolaSequence));  
    break;
  }

  return 0;
}
#endif

Compilation message

gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:68:9: warning: unused variable 'cnt' [-Wunused-variable]
   68 |     int cnt = 0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 7 ms 620 KB Output is correct
8 Correct 7 ms 468 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 9 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 4 ms 340 KB Output is correct
7 Correct 7 ms 596 KB Output is correct
8 Correct 7 ms 468 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 8 ms 596 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 5 ms 372 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 8 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 7 ms 832 KB Output is correct
12 Correct 8 ms 940 KB Output is correct
13 Correct 9 ms 1108 KB Output is correct
14 Correct 7 ms 852 KB Output is correct
15 Correct 18 ms 2044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 244 KB Output is correct
9 Correct 18 ms 780 KB Output is correct
10 Correct 12 ms 724 KB Output is correct
11 Correct 4 ms 448 KB Output is correct
12 Correct 6 ms 596 KB Output is correct
13 Correct 2 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 13 ms 840 KB Output is correct
10 Correct 11 ms 724 KB Output is correct
11 Correct 4 ms 448 KB Output is correct
12 Correct 5 ms 588 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 19 ms 1364 KB Output is correct
15 Correct 18 ms 1580 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 12 ms 1084 KB Output is correct
18 Correct 7 ms 724 KB Output is correct