Submission #71513444


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    int N;
    cin >> N;
    vector<int> P(N);
    for (int i = 0; i < N; i++) {
        cin >> P[i];
        P[i]--;
    }

    vector<int> pos(N);
    for (int i = 0; i < N; i++) {
        pos[P[i]] = i;
    }
    vector<int> parent(N);
    iota(parent.begin(), parent.end(), 0); 
    function<int(int)> find = [&](int x) -> int {
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    };

    auto unite = [&](int x, int y) {
        x = find(x);
        y = find(y);
        if (x != y) {
            parent[y] = x;
        }
    };

    vector<ll> dp(N, 0);
    for (int h = 0; h < N; h++) {
        int u = pos[h]; 
        ll left_dp = 0, right_dp = 0;
        int left_root = -1, right_root = -1;
        if (u > 0 && P[u-1] < h) {
            left_root = find(u-1);
            left_dp = dp[left_root] + abs(u - left_root);
        }
        if (u < N-1 && P[u+1] < h) {
            right_root = find(u+1);
            right_dp = dp[right_root] + abs(u - right_root);
        }
        dp[u] = max(left_dp, right_dp);
        if (left_root != -1) {
            unite(u, left_root);
        }
        if (right_root != -1) {
            unite(u, right_root);
        }
    }
    int start = pos[N-1];
    cout << dp[start];
    return 0;
}

Submission Info

Submission Time
Task F - Cat exercise
User a_legend_cat
Language C++23 (GCC 15.2.0)
Score 550
Code Size 1445 Byte
Status AC
Exec Time 41 ms
Memory 15764 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 2
AC × 40
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3492 KiB
example_01.txt AC 1 ms 3544 KiB
hand_00.txt AC 36 ms 7240 KiB
hand_01.txt AC 37 ms 7084 KiB
hand_02.txt AC 36 ms 7260 KiB
hand_03.txt AC 41 ms 15764 KiB
hand_04.txt AC 36 ms 7256 KiB
hand_05.txt AC 1 ms 3576 KiB
hand_06.txt AC 1 ms 3528 KiB
hand_07.txt AC 37 ms 7316 KiB
random_00.txt AC 41 ms 7116 KiB
random_01.txt AC 40 ms 7304 KiB
random_02.txt AC 40 ms 7268 KiB
random_03.txt AC 41 ms 7236 KiB
random_04.txt AC 41 ms 7332 KiB
random_05.txt AC 41 ms 7204 KiB
random_06.txt AC 41 ms 7116 KiB
random_07.txt AC 41 ms 7332 KiB
random_08.txt AC 41 ms 7240 KiB
random_09.txt AC 41 ms 7260 KiB
random_10.txt AC 39 ms 7260 KiB
random_11.txt AC 40 ms 7240 KiB
random_12.txt AC 41 ms 7260 KiB
random_13.txt AC 40 ms 7264 KiB
random_14.txt AC 38 ms 7240 KiB
random_15.txt AC 41 ms 7116 KiB
random_16.txt AC 40 ms 7184 KiB
random_17.txt AC 39 ms 7184 KiB
random_18.txt AC 40 ms 7300 KiB
random_19.txt AC 39 ms 7184 KiB
random_20.txt AC 39 ms 7184 KiB
random_21.txt AC 38 ms 7116 KiB
random_22.txt AC 40 ms 7260 KiB
random_23.txt AC 39 ms 7116 KiB
random_24.txt AC 40 ms 7304 KiB
random_25.txt AC 39 ms 8676 KiB
random_26.txt AC 41 ms 7304 KiB
random_27.txt AC 39 ms 8264 KiB
random_28.txt AC 40 ms 7780 KiB
random_29.txt AC 41 ms 7556 KiB