Submission #753175

# Submission time Handle Problem Language Result Execution time Memory
753175 2023-06-04T17:24:06 Z adrilen Gondola (IOI14_gondola) C++17
60 / 100
18 ms 2324 KB
#include "gondola.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
constexpr int max_sub_1 = 2.5e5 + 5;

int valid(int n, int inputSeq[])
{
    bool seen_sub_1[max_sub_1] = { 0 };

    auto it = min_element(inputSeq, inputSeq + n);

    int val = *it, pos = it - inputSeq;
    int start_pos = pos;

    bool start = true;
    while (pos != start_pos || start)
    {
        start = false;
        if (inputSeq[pos] == val) 
        {
            val++, pos++;
            if (pos >= n) pos -= n;
            if (val > n) val -= n;
            continue;
        }

        if (inputSeq[pos] > n)
        {
            if (seen_sub_1[inputSeq[pos]]) return 0;
            seen_sub_1[inputSeq[pos]] = true;
            val++, pos++;
            if (pos >= n) pos -= n;
            if (val > n) val -= n;
            continue;
        }

        return 0;
    }


 
    return 1;
}



int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
    int min_val, min_pos;
    auto min_it = min_element(gondolaSeq, gondolaSeq + n);
    min_val = *min_it, min_pos = min_it - gondolaSeq;

    vector <pii> numbers;

    int val = min_val, pos = min_pos;
    
    if (val > n) val = 1;

    int replacement_pos = 0;

    bool start = true;
    while (pos != min_pos || start)
    {
        start = false;
        if (gondolaSeq[pos] == val)
        {
            pos++, val++;
            if (pos >= n) pos -= n;
            if (val > n) val -= n;
            continue;
        }

        if (gondolaSeq[pos] > n)
        {
            numbers.emplace_back(pii(gondolaSeq[pos], val));

            pos++, val++;
            if (pos >= n) pos -= n;
            if (val > n) val -= n;
            continue;
        }

        abort();
    }

    if (numbers.empty()) return 0;

    sort(numbers.begin(), numbers.end());

    int last_val = n;
    for (const pii &p : numbers)
    {
        replacementSeq[replacement_pos++] = p.second;
        last_val++;
        while (last_val < p.first) replacementSeq[replacement_pos++] = last_val++;
    }

    return replacement_pos;
}

constexpr ll mod = 1e9 + 7, max_exp = 1e5 + 5, bits_max_exp = 31 - __builtin_clz(max_exp);

void modf(ll &num)
{
    if (num >= mod) num %= mod;
}

ll binary_exp(ll base, ll exp)
{
    if (exp == 0) return 1;

    ll output = 1;
    for (int i = 0; i < bits_max_exp; i++)
    {
        if (exp & (1 << i))
        {
            output *= base;
            modf(output);
        }

        base *= base;
        modf(base);
    }

    return output;
}


int countReplacement(int n, int inputSeq[])
{
    int validity = valid(n, inputSeq);
    if (validity == 0) return 0;


    int max_el = *max_element(inputSeq, inputSeq + n);
    if (max_el == n) return 1;


    int min_val, min_pos;
    auto min_it = min_element(inputSeq, inputSeq + n);
    min_val = *min_it, min_pos = min_it - inputSeq;

    vector <ll> numbers;

    int val = min_val, pos = min_pos;
    
    if (val > n) val = 1;

    bool start = true;
    while (pos != min_pos || start)
    {
        start = false;
        if (inputSeq[pos] == val)
        {
            pos++, val++;
            if (pos >= n) pos -= n;
            if (val > n) val -= n;
            continue;
        }

        if (inputSeq[pos] > n)
        {
            numbers.emplace_back(inputSeq[pos]);

            pos++, val++;
            if (pos >= n) pos -= n;
            if (val > n) val -= n;
            continue;
        }

        abort();
    }

    if (numbers.empty()) return 0;

    sort(numbers.begin(), numbers.end(), greater<ll>());

    ll last_val = n, next_val;

    ll output = 1;
    for (size_t i = numbers.size(); i > 0; i--)
    {
        next_val = numbers.back();
        numbers.pop_back();
        output *= binary_exp(i, next_val - last_val - 1);
        modf(output);
        last_val = next_val;
    }

    assert(output != 0);

    return (int)output;
}


/*
If all numbers are higher than n, we have another case

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 0 ms 468 KB Output is correct
5 Correct 1 ms 432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 440 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 4 ms 776 KB Output is correct
7 Correct 8 ms 1004 KB Output is correct
8 Correct 7 ms 948 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 8 ms 956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 440 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 4 ms 704 KB Output is correct
7 Correct 9 ms 916 KB Output is correct
8 Correct 6 ms 852 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 9 ms 956 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 6 ms 692 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 10 ms 916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 308 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 8 ms 684 KB Output is correct
12 Correct 11 ms 712 KB Output is correct
13 Correct 12 ms 1364 KB Output is correct
14 Correct 7 ms 700 KB Output is correct
15 Correct 18 ms 2324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 432 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Incorrect 1 ms 468 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 440 KB Output is correct
3 Correct 1 ms 436 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 436 KB Output is correct
7 Incorrect 1 ms 468 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 440 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Incorrect 1 ms 468 KB Output isn't correct
8 Halted 0 ms 0 KB -