結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー ruthenruthen
提出日時 2022-10-14 22:31:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,545 bytes
コンパイル時間 2,520 ms
コンパイル使用メモリ 204,600 KB
実行使用メモリ 16,472 KB
最終ジャッジ日時 2023-09-08 22:38:29
合計ジャッジ時間 7,991 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 71 ms
10,880 KB
testcase_05 AC 56 ms
8,820 KB
testcase_06 AC 10 ms
4,876 KB
testcase_07 AC 12 ms
5,160 KB
testcase_08 AC 72 ms
13,404 KB
testcase_09 AC 54 ms
7,712 KB
testcase_10 AC 23 ms
4,920 KB
testcase_11 AC 51 ms
9,740 KB
testcase_12 AC 35 ms
6,772 KB
testcase_13 AC 40 ms
10,768 KB
testcase_14 AC 30 ms
5,636 KB
testcase_15 AC 44 ms
10,376 KB
testcase_16 AC 32 ms
5,712 KB
testcase_17 AC 26 ms
9,340 KB
testcase_18 AC 42 ms
10,692 KB
testcase_19 AC 88 ms
12,724 KB
testcase_20 AC 17 ms
5,424 KB
testcase_21 AC 36 ms
6,904 KB
testcase_22 AC 31 ms
5,704 KB
testcase_23 AC 64 ms
11,476 KB
testcase_24 AC 100 ms
14,424 KB
testcase_25 AC 97 ms
14,316 KB
testcase_26 AC 100 ms
14,508 KB
testcase_27 AC 95 ms
14,648 KB
testcase_28 AC 99 ms
14,520 KB
testcase_29 AC 95 ms
14,440 KB
testcase_30 AC 97 ms
14,284 KB
testcase_31 AC 96 ms
14,624 KB
testcase_32 AC 98 ms
14,624 KB
testcase_33 AC 99 ms
14,320 KB
testcase_34 AC 43 ms
6,008 KB
testcase_35 AC 35 ms
5,776 KB
testcase_36 AC 35 ms
5,400 KB
testcase_37 AC 32 ms
5,496 KB
testcase_38 AC 39 ms
5,972 KB
testcase_39 AC 29 ms
5,072 KB
testcase_40 AC 29 ms
5,084 KB
testcase_41 AC 29 ms
5,144 KB
testcase_42 AC 29 ms
5,100 KB
testcase_43 AC 29 ms
5,188 KB
testcase_44 AC 49 ms
16,460 KB
testcase_45 AC 50 ms
16,168 KB
testcase_46 AC 48 ms
16,472 KB
testcase_47 AC 48 ms
16,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    V<ll> H(N);
    rep(i, N) cin >> H[i];
    V<V<int>> G1(N), G2(N);
    rep(i, M) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        G1[x].push_back(y);
        G2[N - 1 - y].push_back(N - 1 - x);
    }
    const ll INF = 1LL << 60;
    auto calc = [&INF](V<V<int>> &G, V<ll> &H) -> ll {
        int N = (int)G.size();
        V<ll> dp0(N, -INF), dp1(N, -INF);
        dp0[0] = dp1[0] = 0;
        for (int i = 0; i < N; i++) {
            if (dp0[i] != -INF) {
                for (auto nex : G[i]) {
                    if (H[i] < H[nex]) {
                        dp1[nex] = max(dp1[nex], dp0[i] + H[nex] - H[i]);
                    } else {
                        dp0[nex] = max(dp0[nex], dp0[i]);
                    }
                }
            }
            if (dp1[i] != -INF) {
                for (auto nex : G[i]) {
                    if (H[i] > H[nex]) {
                        dp0[nex] = max(dp0[nex], dp1[i]);
                    }
                }
            }
        }
        return max({dp0[N - 1], dp1[N - 1], -1LL});
    };
    ll ans1 = calc(G1, H);
    reverse(H.begin(), H.end());
    ll ans2 = calc(G2, H);
    cout << ans1 << '\n' << ans2 << '\n';
    return 0;
}
0