結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー sapphire__15sapphire__15
提出日時 2022-09-07 01:07:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,166 bytes
コンパイル時間 865 ms
コンパイル使用メモリ 83,140 KB
実行使用メモリ 12,728 KB
最終ジャッジ日時 2023-09-08 20:07:12
合計ジャッジ時間 8,009 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 122 ms
10,200 KB
testcase_05 AC 96 ms
8,484 KB
testcase_06 AC 18 ms
4,384 KB
testcase_07 AC 22 ms
4,616 KB
testcase_08 AC 122 ms
11,088 KB
testcase_09 AC 98 ms
8,124 KB
testcase_10 AC 48 ms
5,808 KB
testcase_11 AC 90 ms
8,636 KB
testcase_12 AC 67 ms
6,712 KB
testcase_13 AC 73 ms
8,608 KB
testcase_14 AC 57 ms
6,200 KB
testcase_15 AC 75 ms
8,596 KB
testcase_16 AC 62 ms
6,484 KB
testcase_17 AC 48 ms
7,564 KB
testcase_18 AC 76 ms
8,720 KB
testcase_19 AC 146 ms
11,680 KB
testcase_20 AC 32 ms
5,164 KB
testcase_21 AC 66 ms
6,724 KB
testcase_22 AC 63 ms
6,412 KB
testcase_23 AC 110 ms
10,136 KB
testcase_24 AC 159 ms
12,576 KB
testcase_25 AC 161 ms
12,456 KB
testcase_26 AC 166 ms
12,576 KB
testcase_27 AC 163 ms
12,496 KB
testcase_28 AC 163 ms
12,728 KB
testcase_29 AC 165 ms
12,576 KB
testcase_30 AC 169 ms
12,536 KB
testcase_31 AC 160 ms
12,444 KB
testcase_32 AC 162 ms
12,536 KB
testcase_33 AC 162 ms
12,516 KB
testcase_34 AC 87 ms
7,692 KB
testcase_35 AC 78 ms
7,700 KB
testcase_36 AC 75 ms
7,068 KB
testcase_37 AC 71 ms
7,336 KB
testcase_38 AC 79 ms
7,096 KB
testcase_39 AC 65 ms
8,096 KB
testcase_40 AC 65 ms
8,044 KB
testcase_41 AC 63 ms
8,088 KB
testcase_42 AC 64 ms
8,024 KB
testcase_43 AC 63 ms
7,964 KB
testcase_44 AC 93 ms
10,856 KB
testcase_45 AC 95 ms
10,860 KB
testcase_46 AC 91 ms
10,964 KB
testcase_47 AC 94 ms
10,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

using ll = long long;

void chmax(ll &x, ll y) { x = max(x, y); }

ll solve(const vector<ll> &h, const vector<vector<size_t>> &g) {
    vector<ll> dp0(h.size(), -1), dp1(h.size(), -1);
    dp0[0] = 0;
    for(size_t i = 0; i< h.size(); i++) {
        for(auto &j : g[i]) {
            if(i < j) {
                if(h[i] < h[j]) {
                    if(dp0[i] >= 0) chmax(dp1[j], dp0[i] + (h[j] - h[i]));
                }
                else {
                    chmax(dp0[j], max(dp0[i], dp1[i]));
                }
            }
        }
    }
    return max(dp0.back(), dp1.back());
}

int main() {
    int n, m; cin >> n >> m;
    vector<ll> h(n);
    for(int i = 0; i < n; i++) cin >> h[i];
    vector<vector<size_t>> g(n);
    for(int i = 0; i < m; i++) {
        size_t x, y; cin >> x >> y;
        --x; --y;
        g[x].emplace_back(y);
        g[y].emplace_back(x);
    }
    cout << solve(h, g) << endl;

    // reverse
    reverse(begin(h), end(h));
    reverse(begin(g), end(g));
    for(auto &i : g) for(auto &j :i) j = n - j - 1;

    cout << solve(h, g) << endl;
}
0