Submission #588904

# Submission time Handle Problem Language Result Execution time Memory
588904 2022-07-04T07:25:21 Z snasibov05 Gondola (IOI14_gondola) C++14
100 / 100
47 ms 6608 KB
#include "gondola.h"
#include <bits/stdc++.h>

#define ll long long

using namespace std;

int valid(int n, int inputSeq[]){

    int mni = 0;
    set<int> st;
    for (int i = 0; i < n; ++i){
        st.insert(inputSeq[i]);
        if (inputSeq[i] < inputSeq[mni]) mni = i;
    }

    if (st.size() < n) return 0;
    if (inputSeq[mni] > n) return 1;


    int k = mni - 1, x = inputSeq[mni] - 1;
    while (x > 0) {
        if (k == -1) k = n-1;
        if (inputSeq[k] != x && inputSeq[k] <= n) return 0;
        inputSeq[k] = x;
        k--, x--;
    }

    k = mni + 1, x = inputSeq[mni] + 1;
    while (x <= n){
        if (k == n) k = 0;
        if (inputSeq[k] != x && inputSeq[k] <= n) return 0;
        inputSeq[k] = x;
        k++, x++;
    }

    return 1;

}

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

int replacement(int n, int gondolaSeq[], int replacementSeq[]){

    int mni = 0, mxi = 0;
    for (int i = 0; i < n; ++i){
        if (gondolaSeq[i] < gondolaSeq[mni]) mni = i;
        if (gondolaSeq[i] > gondolaSeq[mxi]) mxi = i;
    }

    if (gondolaSeq[mxi] <= n) return 0;

    vector<int> v(n);

    if (gondolaSeq[mni] <= n){
        v[mni] = gondolaSeq[mni];
        int k = mni - 1, x = gondolaSeq[mni] - 1;
        while (x > 0) {
            if (k == -1) k = n-1;
            v[k] = x;
            k--, x--;
        }

        k = mni + 1, x = gondolaSeq[mni] + 1;
        while (x <= n){
            if (k == n) k = 0;
            v[k] = x;
            k++, x++;
        }
    } else{
        for (int i = 0; i < n; ++i) v[i] = i+1;
    }

    vector<pair<int, int>> arr;
    for (int i = 0; i < n; ++i) {
        if (gondolaSeq[i] <= n) continue;
        arr.push_back({gondolaSeq[i], i});
    }

    sort(arr.begin(), arr.end());
    int l = 0;
    for (int i = 0; i < arr.size(); ++i){
        int k;
        if (i == 0) k = n+1;
        else k = arr[i-1].first + 1;

        while (k < arr[i].first) {
            replacementSeq[l] = v[mxi];
            v[mxi] = k;
            l++, k++;
        }

        replacementSeq[l] = v[arr[i].second];
        v[arr[i].second] = arr[i].first;
        l++;
    }

    return l;
}

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

ll binpow(ll n, ll p, ll m){
    if (p == 0) return 1;
    else if (p % 2 == 0) {
        ll k = binpow(n, p / 2, m);
        return k * k % m;
    } else{
        ll k = binpow(n, p-1, m);
        return k * n % m;
    }
}

int countReplacement(int n, int inputSeq[]){

    const int m = 1e9 + 9;
    ll ans = 1;

    int mni = 0;
    set<int> st;
    for (int i = 0; i < n; ++i){
        st.insert(inputSeq[i]);
        if (inputSeq[i] < inputSeq[mni]) mni = i;
    }

    if (st.size() < n) return 0;
    if (inputSeq[mni] > n) ans *= n;

    int k = mni - 1, x = inputSeq[mni] - 1;
    while (x > 0 && inputSeq[mni] <= n) {
        if (k == -1) k = n-1;
        if (inputSeq[k] != x && inputSeq[k] <= n) return 0;
        k--, x--;
    }
    k = mni + 1, x = inputSeq[mni] + 1;
    while (x <= n && inputSeq[mni] <= n){
        if (k == n) k = 0;
        if (inputSeq[k] != x && inputSeq[k] <= n) return 0;
        k++, x++;
    }

    vector<int> arr;
    for (int i = 0; i < n; ++i) {
        if (inputSeq[i] <= n) continue;
        arr.push_back(inputSeq[i]);
    }

    sort(arr.begin(), arr.end());
    for (int i = 0; i < arr.size(); ++i){
        int kk;
        if (i == 0) kk = n+1;
        else kk = arr[i-1] + 1;
        
        ans = ans * binpow(arr.size() - i, arr[i] - kk, m) % m;
    }

    return (int)ans;
}

Compilation message

gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:17:19: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   17 |     if (st.size() < n) return 0;
      |         ~~~~~~~~~~^~~
gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for (int i = 0; i < arr.size(); ++i){
      |                     ~~^~~~~~~~~~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:126:19: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  126 |     if (st.size() < n) return 0;
      |         ~~~~~~~~~~^~~
gondola.cpp:149:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |     for (int i = 0; i < arr.size(); ++i){
      |                     ~~^~~~~~~~~~~~
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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 12 ms 2132 KB Output is correct
7 Correct 23 ms 3668 KB Output is correct
8 Correct 20 ms 3924 KB Output is correct
9 Correct 7 ms 1364 KB Output is correct
10 Correct 24 ms 4524 KB Output is correct
# Verdict Execution time Memory 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 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 10 ms 2132 KB Output is correct
7 Correct 22 ms 3576 KB Output is correct
8 Correct 22 ms 3832 KB Output is correct
9 Correct 6 ms 1376 KB Output is correct
10 Correct 22 ms 4472 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 11 ms 2004 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 29 ms 4644 KB Output is correct
# Verdict Execution time Memory 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 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory 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 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 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
# Verdict Execution time Memory 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 212 KB Output is correct
8 Correct 2 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 10 ms 584 KB Output is correct
12 Correct 8 ms 596 KB Output is correct
13 Correct 12 ms 1232 KB Output is correct
14 Correct 6 ms 596 KB Output is correct
15 Correct 18 ms 2304 KB Output is correct
# Verdict Execution time Memory 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory 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 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 40 ms 4896 KB Output is correct
10 Correct 27 ms 4180 KB Output is correct
11 Correct 9 ms 1808 KB Output is correct
12 Correct 13 ms 2088 KB Output is correct
13 Correct 4 ms 724 KB Output is correct
# Verdict Execution time Memory 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 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
9 Correct 31 ms 4860 KB Output is correct
10 Correct 28 ms 4128 KB Output is correct
11 Correct 10 ms 1820 KB Output is correct
12 Correct 11 ms 1984 KB Output is correct
13 Correct 3 ms 724 KB Output is correct
14 Correct 47 ms 5968 KB Output is correct
15 Correct 46 ms 6608 KB Output is correct
16 Correct 7 ms 1508 KB Output is correct
17 Correct 28 ms 4456 KB Output is correct
18 Correct 15 ms 2772 KB Output is correct