結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー nystnyst
提出日時 2022-10-14 23:44:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,722 bytes
コンパイル時間 1,422 ms
コンパイル使用メモリ 104,004 KB
実行使用メモリ 12,096 KB
最終ジャッジ日時 2023-09-09 00:11:15
合計ジャッジ時間 9,262 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 130 ms
9,176 KB
testcase_05 AC 100 ms
7,332 KB
testcase_06 AC 17 ms
4,384 KB
testcase_07 AC 23 ms
4,508 KB
testcase_08 AC 124 ms
10,888 KB
testcase_09 AC 97 ms
7,036 KB
testcase_10 AC 54 ms
4,504 KB
testcase_11 AC 93 ms
8,064 KB
testcase_12 AC 67 ms
5,868 KB
testcase_13 AC 73 ms
9,048 KB
testcase_14 AC 59 ms
5,176 KB
testcase_15 AC 80 ms
8,756 KB
testcase_16 AC 65 ms
5,044 KB
testcase_17 AC 48 ms
7,920 KB
testcase_18 AC 75 ms
8,976 KB
testcase_19 AC 152 ms
10,724 KB
testcase_20 AC 30 ms
4,692 KB
testcase_21 AC 67 ms
6,120 KB
testcase_22 AC 63 ms
5,260 KB
testcase_23 AC 115 ms
9,572 KB
testcase_24 AC 169 ms
11,820 KB
testcase_25 AC 165 ms
11,836 KB
testcase_26 AC 165 ms
11,920 KB
testcase_27 AC 166 ms
11,888 KB
testcase_28 AC 169 ms
11,960 KB
testcase_29 AC 168 ms
11,884 KB
testcase_30 AC 168 ms
11,864 KB
testcase_31 AC 168 ms
12,096 KB
testcase_32 AC 168 ms
11,956 KB
testcase_33 AC 169 ms
11,880 KB
testcase_34 AC 91 ms
5,612 KB
testcase_35 AC 81 ms
5,400 KB
testcase_36 AC 78 ms
5,416 KB
testcase_37 AC 75 ms
5,384 KB
testcase_38 AC 86 ms
5,500 KB
testcase_39 AC 68 ms
5,624 KB
testcase_40 AC 68 ms
5,544 KB
testcase_41 AC 68 ms
5,612 KB
testcase_42 AC 68 ms
5,356 KB
testcase_43 AC 67 ms
5,704 KB
testcase_44 AC 97 ms
11,548 KB
testcase_45 AC 98 ms
11,748 KB
testcase_46 AC 95 ms
11,684 KB
testcase_47 AC 94 ms
11,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <map>
#include <cmath>
#include <bitset>
#include <string>
#include <queue>
using namespace std;

int main(){
    int n,m;
    cin >> n >> m;
    vector<long long> h(n);
    for(int i=0;i<n;i++) cin >> h[i];
    vector<vector<int>> list(n);

    for(int i=0;i<m;i++){
        int x,y;cin >> x >> y;
        x--;y--;
        list[x].push_back(y);
        list[y].push_back(x);
    }

    vector<vector<long long>> dp(2,vector<long long>(n,-1));
    dp[0][0]=0;
    dp[1][0]=0;
    for(int i =0;i<n;i++){
        if(dp[0][i]!=-1){
            for(auto x:list[i]){
                if(x<i) continue;
                if(h[x]<h[i]) dp[1][x] = max(dp[1][x],dp[0][i]);
            }
        }
        if(dp[1][i]!=-1){
            for(auto x:list[i]){
                if(x<i) continue;
                if(h[i]<h[x]) dp[0][x] = max(dp[0][x],dp[1][i]+h[x]-h[i]);
                if(h[x]<h[i]) dp[1][x] = max(dp[1][x],dp[1][i]);
            }
        }
    }
    cout << max(dp[0][n-1],dp[1][n-1]) << endl;

    dp.clear();
    dp.resize(2);
    dp[0].resize(n,-1);
    dp[1].resize(n,-1);
    dp[0][n-1]=0;
    dp[1][n-1]=0;
    for(int i =n-1;0<=i;i--){
        if(dp[0][i]!=-1){
            for(auto x:list[i]){
                if(i<x) continue;
                if(h[x]<h[i]) dp[1][x] = max(dp[1][x],dp[0][i]);
            }
        }
        if(dp[1][i]!=-1){
            for(auto x:list[i]){
                if(i<x) continue;
                if(h[i]<h[x]) dp[0][x] = max(dp[0][x],dp[1][i]+h[x]-h[i]);
                if(h[x]<h[i]) dp[1][x] = max(dp[1][x],dp[1][i]);
            }
        }
    }
    cout << max(dp[0][0],dp[1][0]) << endl;
}
0