제출 #166971

#제출 시각아이디문제언어결과실행 시간메모리
166971davitmargGondola (IOI14_gondola)C++17
100 / 100
94 ms6836 KiB
/*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

*/

컴파일 시 표준 에러 (stderr) 메시지

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...