Submission #660586

# Submission time Handle Problem Language Result Execution time Memory
660586 2022-11-22T13:27:36 Z Bagritsevich_Stepan Permutation Recovery (info1cup17_permutation) C++14
100 / 100
1376 ms 120120 KB
#include <iostream>
#include <cmath>
#include <unordered_map>

using namespace std;

const long long MOD = 9999999999999937LL;
const long long P = 10;
const int MAX_N = 7e4 + 5;

long long modSum(long long x, long long y) {
    return (x + y) % MOD;
}

long long modSubtraction(long long x, long long y) {
    return (x - y + MOD) % MOD;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    long long lastH = 0;
    long long diff[MAX_N];
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;

        long long h = 0;
        for (int j = 0; j < (int)s.size(); j++) {
            h = modSum(h * P, s[j] - '0');
        }

        diff[i] = modSubtraction(h, lastH);
        lastH = h;
    }

    const int len = (int)sqrt(n) + 1;
    long long hSum = 1;
    long long need[MAX_N];
    unordered_map<long long, int> st[MAX_N];

    for (int i = 0; i < n; i++) {
        need[i] = modSubtraction(hSum, diff[i]);
        st[i / len][need[i]] = i;

        hSum = modSum(hSum, diff[i]);
    }

    bool stat[MAX_N];
    long long push[MAX_N];
    int result[MAX_N];
    fill(stat, stat + MAX_N, true);
    fill(push, push + MAX_N, 0LL);
    fill(result, result + MAX_N, 0);

    const int maxSegmIndex = (n - 1) / len;
    for (int j = n; j > 0; j--) {
        int pos = 0;
        for (int index = maxSegmIndex; index >= 0; index--) {
            if (st[index].find(push[index]) != st[index].end()) {
                pos = st[index][push[index]];
                break;
            }
        }

        result[pos] = j;
        stat[pos] = false;

        const int segmIndex = pos / len;
        const int l = segmIndex * len;
        const int r = min(n, l + len);
        st[segmIndex].clear();

        for (int i = l; i < r; i++) {
            if (!stat[i]) {
                continue;
            }
            if (i > pos) {
                need[i] = modSubtraction(need[i], diff[pos]);
            }
            st[segmIndex][need[i]] = i;
        }

        for (int ind = segmIndex + 1; ind <= maxSegmIndex; ind++) {
            push[ind] = modSum(push[ind], diff[pos]);
        }
    }

    for (int i = 0; i < n; i++) {
        cout << result[i] << ' ';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 4 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 4 ms 6100 KB Output is correct
3 Correct 5 ms 6152 KB Output is correct
4 Correct 5 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 4 ms 6100 KB Output is correct
3 Correct 5 ms 6152 KB Output is correct
4 Correct 5 ms 6100 KB Output is correct
5 Correct 426 ms 12320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 4 ms 6100 KB Output is correct
3 Correct 5 ms 6152 KB Output is correct
4 Correct 5 ms 6100 KB Output is correct
5 Correct 426 ms 12320 KB Output is correct
6 Correct 893 ms 19812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 4 ms 6100 KB Output is correct
3 Correct 5 ms 6152 KB Output is correct
4 Correct 5 ms 6100 KB Output is correct
5 Correct 426 ms 12320 KB Output is correct
6 Correct 893 ms 19812 KB Output is correct
7 Correct 860 ms 25792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 4 ms 6100 KB Output is correct
3 Correct 5 ms 6152 KB Output is correct
4 Correct 5 ms 6100 KB Output is correct
5 Correct 426 ms 12320 KB Output is correct
6 Correct 893 ms 19812 KB Output is correct
7 Correct 860 ms 25792 KB Output is correct
8 Correct 687 ms 93704 KB Output is correct
9 Correct 1208 ms 85300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6100 KB Output is correct
2 Correct 4 ms 6100 KB Output is correct
3 Correct 5 ms 6152 KB Output is correct
4 Correct 5 ms 6100 KB Output is correct
5 Correct 426 ms 12320 KB Output is correct
6 Correct 893 ms 19812 KB Output is correct
7 Correct 860 ms 25792 KB Output is correct
8 Correct 687 ms 93704 KB Output is correct
9 Correct 1208 ms 85300 KB Output is correct
10 Correct 1376 ms 120120 KB Output is correct