Submission #166971

# Submission time Handle Problem Language Result Execution time Memory
166971 2019-12-05T05:50:50 Z davitmarg Gondola (IOI14_gondola) C++17
100 / 100
94 ms 6836 KB
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000009ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

#ifndef death
#include "gondola.h"
#endif

int valid(int n, int inputSeq[])
{
    vector<int> a, b;
    for (int i = 0; i < n; i++)
        a.PB(inputSeq[i]);
    b = a;
    for (int i = 0; i < n; i++)
    {
        if (a[i] > n)
            continue;
        for (int j = 0; j < n; j++)
            b[(a[i] - 1 + j) % n] = a[(i + j) % n];
        break;
    }
    a = b;
    map<int, int> used;
    for (int i = 0; i < n; i++)
    {
        if ((a[i] <= n && i + 1 != a[i]) || used[a[i]])
            return 0;
        used[a[i]] = 1;
    }
    return 1;
}

LL binpow(LL a, LL n)
{
    if (n == 0)
        return 1;
    if (n % 2 == 0)
        return binpow((a * a) % mod, n / 2);
    return (a * binpow(a, n - 1)) % mod;
}

int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
    vector<int> ans;
    vector<pair<int, int>> nxt;
    vector<int> a, b;
    for (int i = 0; i < n; i++)
        a.PB(gondolaSeq[i]);

    b = a;
    for (int i = 0; i < n; i++)
    {
        if (a[i] > n)
            continue;
        for (int j = 0; j < n; j++)
            b[(a[i] - 1 + j) % n] = a[(i + j) % n];
        break;
    }
    a = b;

    for (int i = 0; i < n; i++)
        if (a[i] > n)
            nxt.PB(MP(a[i], i));

    sort(all(nxt));
    while (!nxt.empty())
    {
        int pos = nxt.back().second;
        nxt.pop_back();

        int mn = n;
        if (!nxt.empty())
            mn = nxt.back().first;

        for (int i = a[pos]; i > mn + 1; i--)
            ans.PB(i - 2);
        ans.PB(pos);
    }
    reverse(all(ans));
    for (int i = 0; i < ans.size(); i++)
        replacementSeq[i] = ans[i] + 1;
    return ans.size();
}

int countReplacement(int n, int inputSeq[])
{
    if (!valid(n, inputSeq))
        return 0;
    vector<pair<int, int>> nxt;
    vector<int> a, b;
    for (int i = 0; i < n; i++)
        a.PB(inputSeq[i]);

    for (int i = 0; i < n; i++)
        if (a[i] > n)
            nxt.PB(MP(a[i], i));
    int last = n;

    LL ans = 1;
    if (nxt.size() == n)
        ans = n;
    sort(all(nxt));
    for (int i = 0; i < nxt.size(); i++)
    {
        LL cnt = nxt.size() - i;
        LL x = nxt[i].first - last;
        //cout << x << " : " << cnt << endl;
        ans *= binpow(cnt, x - 1);
        ans %= mod;
        last = nxt[i].first;
    }
    return ans;
}

#ifdef death

int main()
{
    int N, A[10002], B[10002];
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> A[i];
    N = countReplacement(N, A);
    cout << N << endl;
    return 0;
}

#endif

/*

7
5 6 7 1 2 3 4


7
5 6 7 1 2 10 4 

7
2 3 4 9 6 7 1

1 2 3 4 9 6 7

*/

Compilation message

gondola.cpp: In function 'int replacement(int, int*, int*)':
gondola.cpp:102:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < ans.size(); i++)
                     ~~^~~~~~~~~~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:122:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (nxt.size() == n)
         ~~~~~~~~~~~^~~~
gondola.cpp:125:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < nxt.size(); i++)
                     ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 8 ms 504 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 21 ms 2564 KB Output is correct
7 Correct 16 ms 1520 KB Output is correct
8 Correct 44 ms 4556 KB Output is correct
9 Correct 13 ms 1656 KB Output is correct
10 Correct 52 ms 5360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 22 ms 2548 KB Output is correct
7 Correct 16 ms 1520 KB Output is correct
8 Correct 44 ms 4592 KB Output is correct
9 Correct 13 ms 1656 KB Output is correct
10 Correct 52 ms 5232 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 9 ms 892 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 17 ms 1392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 380 KB Output is correct
11 Correct 14 ms 1392 KB Output is correct
12 Correct 16 ms 1648 KB Output is correct
13 Correct 20 ms 1652 KB Output is correct
14 Correct 14 ms 1392 KB Output is correct
15 Correct 27 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 292 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 60 ms 5232 KB Output is correct
10 Correct 47 ms 4340 KB Output is correct
11 Correct 19 ms 1784 KB Output is correct
12 Correct 23 ms 2040 KB Output is correct
13 Correct 6 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 396 KB Output is correct
6 Correct 2 ms 276 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 64 ms 5080 KB Output is correct
10 Correct 50 ms 4324 KB Output is correct
11 Correct 20 ms 1784 KB Output is correct
12 Correct 24 ms 2040 KB Output is correct
13 Correct 6 ms 760 KB Output is correct
14 Correct 76 ms 6000 KB Output is correct
15 Correct 94 ms 6836 KB Output is correct
16 Correct 16 ms 1528 KB Output is correct
17 Correct 57 ms 4696 KB Output is correct
18 Correct 32 ms 2804 KB Output is correct