Submission #434300

# Submission time Handle Problem Language Result Execution time Memory
434300 2021-06-20T21:54:01 Z illyakr Gondola (IOI14_gondola) C++14
100 / 100
45 ms 7608 KB
#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
#define ll long long

unordered_map<int, int> used;
int valid(int n, int inputSeq[]) {
    int mn = 1010101010, sv = -1;
    for (int i = 0; i < n; i++) {
        if (used[inputSeq[i]])return 0;
        used[inputSeq[i]] = true;
        if (mn > inputSeq[i]) {
            mn = inputSeq[i];
            sv = i;
        }
    }
    if (mn > n)return 1;
    int go = mn;
    for (int i = sv - 1; i >= 0; i--) {
        go--;
        if (go < 1)go += n;
        if (inputSeq[i] > n)continue;
        if (inputSeq[i] != go)return 0;
    }
    for (int i = n - 1; i > sv; i--) {
        go--;
        if (go < 1)go += n;
        if (inputSeq[i] > n)continue;
        if (inputSeq[i] != go)return 0;
    }


    return 1;
}

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

int put[301010];
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
    int mn = 1010101010, sv = -1;
    for (int i = 0; i < n; i++) {
        if (mn > gondolaSeq[i]) {
            mn = gondolaSeq[i];
            sv = i;
        }
    }
    int mx = 0;
    for (int i = 0; i < n; i++)mx = max(mx, gondolaSeq[i]);

    if (mn > n) {
        vector<int> use;
        for (int i = n + 1; i <= mx; i++)
            use.push_back(i);
        reverse(use.begin(), use.end());

        vector<pair<int, int> > sv;
        for (int i = 0; i < n; i++)
            sv.push_back({gondolaSeq[i], i});
        sort(sv.begin(), sv.end(), [](pair<int, int> q, pair<int, int> w) {
             return q.first < w.first;
        });

        int sz = 0;
        for (int i = 0; i < n; i++) {
            replacementSeq[sz++] = sv[i].second + 1;
            int el = use[use.size() - 1];
            use.pop_back();
            while (!use.empty() && el != sv[i].first) {
                replacementSeq[sz++] = el;
                el = use[use.size() - 1];
                use.pop_back();
            }
        }
        return sz;
    }
    vector<int> use;
    for (int i = n + 1; i <= mx; i++)
        use.push_back(i);
    reverse(use.begin(), use.end());

    vector<pair<int, int> > SV;

    int go = mn;
    for (int i = sv - 1; i >= 0; i--) {
        go--;
        if (go < 1)go += n;
        if (gondolaSeq[i] > n){SV.push_back({gondolaSeq[i], go});continue;}
    }
    for (int i = n - 1; i > sv; i--) {
        go--;
        if (go < 1)go += n;
        if (gondolaSeq[i] > n){SV.push_back({gondolaSeq[i], go});continue;}
    }

    sort(SV.begin(), SV.end(), [](pair<int, int> q, pair<int, int> w) {
         return q.first < w.first;
    });
//    for (auto [first, second] : SV) {
//        cout << first << " " << second << " << " << endl;
//    }
    int sz = 0;
    for (auto [first, second] : SV) {
        replacementSeq[sz++] = second;
        int el = use[use.size() - 1];
        use.pop_back();
        while (!use.empty() && el != first) {
            replacementSeq[sz++] = el;
            el = use[use.size() - 1];
            use.pop_back();
        }
    }
    return sz;
}

//----------------------
const ll mod = 1000000009;

ll bp(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1)res *= a;
        a *= a;
        b /= 2;

        res %= mod;
        a %= mod;
    }
    return res;
}
ll ans = 1;
int countReplacement(int n, int inputSeq[]) {
    if (!valid(n, inputSeq))return 0;
    ll mn = 1010101010, sv = -1;
    for (ll i = 0; i < n; i++) {
        if (mn > inputSeq[i]) {
            mn = inputSeq[i];
            sv = i;
        }
    }
    if (mn <= n) {
        vector<ll> can = {0};
        for (ll i = 0; i < n; i++) {
            if (inputSeq[i] > n)
                can.push_back(inputSeq[i] - n);
        }
        sort(can.begin(), can.end());
        for (ll i = 1; i < can.size(); i++) {
            ans *= bp((ll)(can.size() - i), (ll)(can[i] - can[i - 1] - 1));
            ans %= mod;
        }
        return ans;
    } else {
        vector<ll> can = {0};
        for (ll i = 0; i < n; i++) {
            if (inputSeq[i] > n)
                can.push_back(inputSeq[i] - n);
        }
        sort(can.begin(), can.end());
        for (ll i = 1; i < can.size(); i++) {
            ans *= bp((ll)(can.size() - i), (ll)(can[i] - can[i - 1] - 1));
            ans %= mod;
        }
        ll P = n;
        ans *= P;
        ans %= mod;
        return ans;
    }
}

/**
*/

Compilation message

gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:102:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  102 |     for (auto [first, second] : SV) {
      |               ^
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:147:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |         for (ll i = 1; i < can.size(); i++) {
      |                        ~~^~~~~~~~~~~~
gondola.cpp:159:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |         for (ll i = 1; i < can.size(); i++) {
      |                        ~~^~~~~~~~~~~~
gondola.cpp:133:25: warning: variable 'sv' set but not used [-Wunused-but-set-variable]
  133 |     ll mn = 1010101010, sv = -1;
      |                         ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 8 ms 1960 KB Output is correct
7 Correct 10 ms 588 KB Output is correct
8 Correct 18 ms 3488 KB Output is correct
9 Correct 5 ms 1484 KB Output is correct
10 Correct 17 ms 3912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1908 KB Output is correct
7 Correct 10 ms 664 KB Output is correct
8 Correct 15 ms 3420 KB Output is correct
9 Correct 5 ms 1484 KB Output is correct
10 Correct 21 ms 3796 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 9 ms 1840 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 23 ms 5268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 9 ms 588 KB Output is correct
12 Correct 10 ms 588 KB Output is correct
13 Correct 16 ms 1492 KB Output is correct
14 Correct 10 ms 588 KB Output is correct
15 Correct 31 ms 2480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 26 ms 4444 KB Output is correct
10 Correct 22 ms 3884 KB Output is correct
11 Correct 8 ms 1804 KB Output is correct
12 Correct 10 ms 1960 KB Output is correct
13 Correct 3 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 25 ms 4528 KB Output is correct
10 Correct 22 ms 3988 KB Output is correct
11 Correct 8 ms 1832 KB Output is correct
12 Correct 10 ms 1960 KB Output is correct
13 Correct 3 ms 716 KB Output is correct
14 Correct 33 ms 5436 KB Output is correct
15 Correct 45 ms 7608 KB Output is correct
16 Correct 7 ms 1612 KB Output is correct
17 Correct 25 ms 4776 KB Output is correct
18 Correct 14 ms 2908 KB Output is correct