39392023-03-05 23:40:11zsomborMágikus sorozatcpp17Accepted 100/100165ms88640 KiB
#include <iostream>
#include <vector>
using namespace std;

int n, done = 1, a, b, cnt = 0;
vector <int> z(3e5, 0);
vector <int> p(3e5, 0);
vector <vector <int>> dif(3e5);
vector <int> root;
vector <vector <bool>> v(800, vector<bool>(3e5, true));
vector <int> val(3e5, 0);

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> z[i];
        p[i] = i;
    }
    for (int i = 1; i < n; i++) {
        for (int j = max(i, done); j < i + z[i]; j++) {
            p[j] = j - i;
        }
        done = max(done, i + z[i]);
        p[i] = p[p[i]];
    }
    for (int i = 1; i < n; i++) {
        a = z[i]; b = i + z[i];
        if (b < n) dif[a].push_back(b);
    }
    for (int i = 0; i < n; i++) {
        if (p[i] != i) { p[i] = p[p[i]]; continue; }
        a = cnt;
        for (int j = cnt - 1; j >= 0; j--) if (v[j][i]) a = j;
        if (a == cnt) { root.push_back(i); cnt++; val[i] = cnt; }
        p[i] = root[a];
        for (int j : dif[i]) v[a][j] = false;
    }
    for (int i = 0; i < n; i++) cout << val[p[i]] << " ";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted28ms81472 KiB
2Accepted32ms82092 KiB
subtask215/15
3Accepted29ms81892 KiB
4Accepted37ms82100 KiB
5Accepted30ms82400 KiB
subtask315/15
6Accepted28ms82352 KiB
7Accepted28ms82356 KiB
8Accepted28ms82648 KiB
9Accepted28ms82912 KiB
10Accepted35ms83032 KiB
subtask430/30
11Accepted37ms82908 KiB
12Accepted29ms83164 KiB
13Accepted37ms83372 KiB
14Accepted37ms83584 KiB
subtask540/40
15Accepted128ms88468 KiB
16Accepted128ms86716 KiB
17Accepted129ms87416 KiB
18Accepted127ms87352 KiB
19Accepted136ms87312 KiB
20Accepted149ms88112 KiB
21Accepted153ms88012 KiB
22Accepted150ms87952 KiB
23Accepted145ms88056 KiB
24Accepted131ms87972 KiB
25Accepted133ms87824 KiB
26Accepted135ms88340 KiB
27Accepted109ms86304 KiB
28Accepted123ms86828 KiB
29Accepted128ms88640 KiB
30Accepted165ms84396 KiB
31Accepted146ms86928 KiB
32Accepted128ms86880 KiB