結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー milanis48663220milanis48663220
提出日時 2022-10-14 22:00:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 2,573 bytes
コンパイル時間 1,294 ms
コンパイル使用メモリ 126,080 KB
実行使用メモリ 17,216 KB
最終ジャッジ日時 2023-09-08 21:16:31
合計ジャッジ時間 5,696 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 63 ms
12,896 KB
testcase_05 AC 50 ms
10,216 KB
testcase_06 AC 11 ms
5,220 KB
testcase_07 AC 14 ms
5,668 KB
testcase_08 AC 67 ms
15,148 KB
testcase_09 AC 49 ms
9,216 KB
testcase_10 AC 25 ms
5,780 KB
testcase_11 AC 49 ms
11,188 KB
testcase_12 AC 34 ms
7,520 KB
testcase_13 AC 43 ms
12,096 KB
testcase_14 AC 29 ms
6,316 KB
testcase_15 AC 46 ms
11,964 KB
testcase_16 AC 31 ms
6,720 KB
testcase_17 AC 31 ms
10,656 KB
testcase_18 AC 44 ms
12,296 KB
testcase_19 AC 76 ms
15,264 KB
testcase_20 AC 17 ms
6,112 KB
testcase_21 AC 34 ms
8,108 KB
testcase_22 AC 30 ms
6,528 KB
testcase_23 AC 59 ms
13,388 KB
testcase_24 AC 85 ms
17,036 KB
testcase_25 AC 84 ms
17,076 KB
testcase_26 AC 84 ms
17,036 KB
testcase_27 AC 85 ms
17,068 KB
testcase_28 AC 84 ms
16,980 KB
testcase_29 AC 84 ms
17,192 KB
testcase_30 AC 86 ms
17,044 KB
testcase_31 AC 85 ms
17,084 KB
testcase_32 AC 85 ms
17,132 KB
testcase_33 AC 85 ms
17,132 KB
testcase_34 AC 41 ms
7,332 KB
testcase_35 AC 37 ms
6,868 KB
testcase_36 AC 36 ms
6,760 KB
testcase_37 AC 35 ms
6,768 KB
testcase_38 AC 39 ms
7,264 KB
testcase_39 AC 30 ms
6,668 KB
testcase_40 AC 30 ms
6,572 KB
testcase_41 AC 30 ms
6,648 KB
testcase_42 AC 30 ms
6,856 KB
testcase_43 AC 29 ms
6,776 KB
testcase_44 AC 61 ms
17,216 KB
testcase_45 AC 60 ms
17,048 KB
testcase_46 AC 60 ms
17,112 KB
testcase_47 AC 61 ms
17,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

const ll inf = 1e18;

ll solve(vector<ll> h, vector<int> x, vector<int> y){
    int n = h.size(), m = x.size();
    vector<vector<int>> g(n);
    for(int i = 0; i < m; i++){
        if(x[i] < y[i]) g[x[i]].push_back(y[i]);
        else g[y[i]].push_back(x[i]);
    }
    auto dp = vec2d(n, 2, -inf);
    dp[0][0] = 0;
    dp[0][1] = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < 2; j++){
            if(dp[i][j] == -inf) continue;
            for(int to: g[i]){
                if(to < i) continue;
                if(h[i] > h[to]){ // 降りる
                    chmax(dp[to][0], dp[i][j]);
                }else{
                    if(j == 0) chmax(dp[to][1], dp[i][j]+h[to]-h[i]);
                }
            }
        }
        // cout << dp[i][0] << ' ' << dp[i][1] << endl;
    }
    ll mx = max(dp[n-1][0], dp[n-1][1]);
    if(mx == -inf) return -1;
    return mx;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    vector<ll> h(n);
    for(int i = 0; i < n; i++) cin >> h[i];
    vector<int> x(m), y(m);
    for(int i = 0; i < m; i++){
        cin >> x[i] >> y[i]; x[i]--; y[i]--;
    }
    cout << solve(h, x, y) << endl;
    reverse(h.begin(), h.end());
    for(int i = 0; i < m; i++){
        x[i] = n-1-x[i];
        y[i] = n-1-y[i];
    }
    cout << solve(h, x, y) << endl;
}
0