Submission #535358

# Submission time Handle Problem Language Result Execution time Memory
535358 2022-03-10T05:57:36 Z KoD JOI15_aaqqz (JOI15_aaqqz) C++17
100 / 100
1225 ms 44920 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

void setmax(int& x, const int y) {
    if (x < y) {
        x = y;
    }
}

int solve(const int N, const int C, const vector<int>& A) {
    vector<vector<int>> idx(C);
    for (int i = 0; i < N; ++i) {
        idx[A[i]].push_back(i);
    }
    vector<int> taken(C);
    const auto reset = [&](const int lowb) {
        for (int i = 0; i < C; ++i) {
            taken[i] = std::lower_bound(idx[i].begin(), idx[i].end(), lowb) - idx[i].begin();
        }
    };
    const auto empty = [&](const int x) {
        return taken[x] == (int)idx[x].size();
    };
    const auto pop = [&](const int x) {
        return idx[x][taken[x]++];
    };

    int ret = 0;
    vector add(N, vector<int>(N + 2));

    const auto process = [&](const int left, const int right, const int except) {
        reset(right);
        vector<char> type(N);
        vector<pair<int, int>> consider;
        for (int i = left - 1, last = 0; i >= 0; --i) {
            if (A[i] < last or empty(A[i])) {
                break;
            }
            const int j = pop(A[i]);
            type[j] = 1;
            consider.emplace_back(i, j);
            last = A[i];
        }

        for (int i = right; i < N; ++i) {
            if (type[i] == 0 and A[i] == except) {
                type[i] = 2;
            }
        }

        vector<int> bad(C + 1, N);
        for (int i = right; i < N; ++i) {
            if (type[i] == 0 and bad[A[i] + 1] == N) {
                bad[A[i] + 1] = i;
            }
        }
        for (int i = 0; i < C; ++i) {
            bad[i + 1] = std::min(bad[i + 1], bad[i]);
        }
        
        vector<int> sum(N + 1);
        for (int i = 0; i < N; ++i) {
            sum[i + 1] = sum[i] + (type[i] == 2);
        }

        vector<int> nearest(N + 1, N);
        for (int i = N - 1; i >= 0; --i) {
            nearest[i] = type[i] != 2 ? i : nearest[i + 1];
        }

        int must = right;
        for (const auto& [l, r] : consider) {
            setmax(must, r + 1);
            if (bad[A[l]] < must) {
                break;
            }
            setmax(ret, 2 * (left - l) + sum[bad[A[l]]] - sum[right] + right - left);
            if (sum[must] - sum[right] + left - l == must - right) {
                add[l][must] += 1;
                add[l][nearest[must] + 1] -= 1;
            }
        }
    };

    for (int i = 1; i < N; ++i) {
        process(i, i, A[i - 1]);
        for (int j = i; j < N; ++j) {
            if (A[i - 1] > A[j]) {
                process(i, i, A[j]);
                break;
            }
        }
    }

    vector make(N, vector<char>(N + 1));

    for (int i = 0; i < N; ++i) {
        int l = i, r = i + 1;
        while (l > 0 and r < N and A[l - 1] == A[r]) {
            l -= 1;
            r += 1;
        }
        process(l, r, -1);
        make[i][i + 1] = true;
    }
    for (int i = 1; i < N; ++i) {
        int l = i, r = i;
        while (l > 0 and r < N and A[l - 1] == A[r]) {
            l -= 1;
            r += 1;
        }
        process(l, r, -1);
        make[i][i] = true;
    }

    for (int i = 0; i < N; ++i) {
        int sum = 0;
        for (int j = 0; j <= N; ++j) {
            sum += add[i][j];
            make[i][j] = sum > 0;
        }
    }
    for (int len = 1; len < N; ++len) {
        for (int l = 1; l + len < N; ++l) {
            const int r = l + len;
            if (make[l][r] and A[l - 1] == A[r]) {
                make[l - 1][r + 1] = true;
            }
        }
    }
    for (int i = 1; i <= N; ++i) {
        for (int j = 0; j < i; ++j) {
            if (make[j][i]) {
                setmax(ret, i - j);
            }
        }
    }
    return ret;
}

int main() {
    int N, C;
    std::cin >> N >> C;
    vector<int> A(N), freq(C);
    for (auto& x : A) {
        std::cin >> x;
        x -= 1;
        freq[x] += 1;
    }
    int ans = *std::max_element(freq.begin(), freq.end());
    setmax(ans, solve(N, C, A));
    std::reverse(A.begin(), A.end());
    for (auto& x : A) {
        x = C - x - 1;
    }
    setmax(ans, solve(N, C, A));
    std::cout << ans << '\n';
}
# 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 212 KB Output is correct
5 Correct 1 ms 300 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 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 304 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 308 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 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 212 KB Output is correct
5 Correct 1 ms 300 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 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 304 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 308 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1034 ms 44768 KB Output is correct
39 Correct 699 ms 44736 KB Output is correct
40 Correct 771 ms 44768 KB Output is correct
41 Correct 769 ms 44768 KB Output is correct
42 Correct 766 ms 44776 KB Output is correct
43 Correct 668 ms 44752 KB Output is correct
44 Correct 679 ms 44812 KB Output is correct
45 Correct 1121 ms 44872 KB Output is correct
46 Correct 1094 ms 44788 KB Output is correct
47 Correct 1058 ms 44900 KB Output is correct
48 Correct 1082 ms 44844 KB Output is correct
49 Correct 1118 ms 44800 KB Output is correct
50 Correct 1064 ms 44888 KB Output is correct
51 Correct 1073 ms 44884 KB Output is correct
52 Correct 1119 ms 44804 KB Output is correct
53 Correct 635 ms 44752 KB Output is correct
54 Correct 629 ms 44744 KB Output is correct
55 Correct 591 ms 44840 KB Output is correct
56 Correct 1225 ms 44804 KB Output is correct
57 Correct 1129 ms 44776 KB Output is correct
58 Correct 1014 ms 44860 KB Output is correct
59 Correct 968 ms 44840 KB Output is correct
60 Correct 1079 ms 44920 KB Output is correct
61 Correct 1108 ms 44760 KB Output is correct
62 Correct 1079 ms 44840 KB Output is correct
63 Correct 1060 ms 44876 KB Output is correct
64 Correct 675 ms 44808 KB Output is correct
65 Correct 694 ms 44888 KB Output is correct
66 Correct 781 ms 44804 KB Output is correct
67 Correct 789 ms 44832 KB Output is correct