답안 #1027487

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1027487 2024-07-19T07:02:49 Z dozer 곤돌라 (IOI14_gondola) C++14
100 / 100
41 ms 7532 KB
#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define mid (l + r) / 2
#define LL node * 2
#define RR node * 2 + 1
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define ll long long
#define MAXN 200005


const int modulo = 1e9+9;
const ll INF = 2e18 + 7;


int valid(int n, int inputSeq[])
{
    set<int> s;
    vector<int> v(n, 0);
    for (int i = 0; i < n; i++){
        int curr = inputSeq[i];
        if (curr <= n) v[i] = curr;
        s.insert(curr);
    }

    if (s.size() < n) return 0;
    int todo = -1;
    for (int i = 0; i < n; i++)
        if (v[i] != 0) todo = i;
    if (todo == -1) return 1;
    vector<int> res(n, 0);
    int it = todo, val = v[todo];
    do{
        res[it] = val;
        val++, it++;
        it %= n;
        val = (val - 1) % n + 1;
    }while(it != todo);

    for (int i = 0; i < n; i++)
        if (v[i] != 0 && res[i] != v[i]) return 0;
    return 1;
}

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

int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
    vector<int> v(n, 0);
    for (int i = 0; i < n; i++){
        int curr = gondolaSeq[i];
        if (curr <= n) v[i] = curr;
    }

    int todo = -1;
    for (int i = 0; i < n; i++)
        if (v[i] != 0) todo = i;

    vector<int> res(n, 0);
    int it = 0, val = 1;
    if (todo != -1){
        it = todo, val = v[todo];
    }
    if (todo == -1) todo = 0;
    do{
        res[it] = val;
        val++, it++;
        it %= n;
        val = (val - 1) % n + 1;
    }while(it != todo);

    vector<pii> td;
    for (int i = 0; i < n; i++){
        if (v[i] == 0){
            td.pb({gondolaSeq[i], res[i]});
            replacementSeq[gondolaSeq[i] - (n + 1)] = res[i];
        }
    }
    if (td.empty()) return 0;
    sort(td.begin(), td.end());
    pii tmp = td.back();

    int last = tmp.nd;
    replacementSeq[tmp.st - (n + 1)] = 0;
    for (int i = n + 1; i <= tmp.st; i++){
        if (replacementSeq[i - (n + 1)] == 0){
            replacementSeq[i - (n + 1)] = last;
            last = i;
        }
    }
    return tmp.st - n;
}

//----------------------
int mul(ll a, ll b){
    return (a * b) % modulo;
}

int add(ll a, ll b){
    if (a + b < modulo) return a + b;
    return a + b - modulo;
}

int fe(ll a, ll b){
    if (b == 0) return 1;
    if (b % 2) return mul(a, fe(a, b - 1));
    ll tmp = fe(a, b / 2);
    return mul(tmp, tmp);
}

int countReplacement(int n, int inputSeq[])
{
    set<int> s;
    vector<int> v(n, 0);
    for (int i = 0; i < n; i++){
        int curr = inputSeq[i];
        if (curr <= n) v[i] = curr;
        s.insert(curr);
    }

    if (s.size() < n) return 0;

    int todo = -1;
    for (int i = 0; i < n; i++)
        if (v[i] != 0) todo = i;

    vector<int> res(n, 0);
    int it = 0, val = 1;
    int flag = 0;
    if (todo != -1){
        it = todo, val = v[todo];
    }
    if (todo == -1) flag = 1, todo = 0;
    do{
        res[it] = val;
        val++, it++;
        it %= n;
        val = (val - 1) % n + 1;
    }while(it != todo);


    for (int i = 0; i < n; i++){
        if (v[i] != 0 && v[i] != res[i]) return 0;
    }


    vector<int> td;
    for (int i = 0; i < n; i++){
        if (v[i] == 0){
            td.pb(inputSeq[i]);
        }
    }
    if (td.empty()) return 1;

    int ans = 1;

    sort(td.begin(), td.end());
    
    int lst = n;
    int m = td.size();
    for (int i = 0; i < m; i++){
        ans = mul(ans, fe(m - i, td[i] - lst - 1));
        lst = td[i];
    }
    if (flag) ans = mul(ans, n);
    return ans;
}
/*
int gondolaSequence[100001];
int replacementSequence[250001];

int main()
{
  fileio();
  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;
}
*/

Compilation message

gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:33:18: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   33 |     if (s.size() < n) return 0;
      |         ~~~~~~~~~^~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:128:18: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  128 |     if (s.size() < n) return 0;
      |         ~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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
# 결과 실행 시간 메모리 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 10 ms 2652 KB Output is correct
7 Correct 20 ms 3932 KB Output is correct
8 Correct 14 ms 4504 KB Output is correct
9 Correct 5 ms 1648 KB Output is correct
10 Correct 19 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 2612 KB Output is correct
7 Correct 22 ms 3932 KB Output is correct
8 Correct 15 ms 4444 KB Output is correct
9 Correct 5 ms 1628 KB Output is correct
10 Correct 18 ms 5124 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 10 ms 2392 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 29 ms 5456 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 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 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 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 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 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 11 ms 1260 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 10 ms 1496 KB Output is correct
14 Correct 12 ms 1116 KB Output is correct
15 Correct 22 ms 2388 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 1 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 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 1 ms 372 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 27 ms 5504 KB Output is correct
10 Correct 22 ms 4692 KB Output is correct
11 Correct 10 ms 2052 KB Output is correct
12 Correct 9 ms 2396 KB Output is correct
13 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 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 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 33 ms 5544 KB Output is correct
10 Correct 23 ms 4692 KB Output is correct
11 Correct 9 ms 1884 KB Output is correct
12 Correct 10 ms 2396 KB Output is correct
13 Correct 2 ms 856 KB Output is correct
14 Correct 41 ms 6688 KB Output is correct
15 Correct 40 ms 7532 KB Output is correct
16 Correct 7 ms 1628 KB Output is correct
17 Correct 24 ms 5168 KB Output is correct
18 Correct 22 ms 3164 KB Output is correct