結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー kyushu1996kyushu1996
提出日時 2022-10-27 02:39:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 1,540 bytes
コンパイル時間 1,286 ms
コンパイル使用メモリ 114,080 KB
実行使用メモリ 25,516 KB
最終ジャッジ日時 2023-09-17 19:40:13
合計ジャッジ時間 10,569 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 152 ms
20,640 KB
testcase_05 AC 133 ms
17,320 KB
testcase_06 AC 14 ms
5,764 KB
testcase_07 AC 17 ms
6,520 KB
testcase_08 AC 117 ms
20,588 KB
testcase_09 AC 153 ms
17,220 KB
testcase_10 AC 126 ms
12,988 KB
testcase_11 AC 88 ms
15,868 KB
testcase_12 AC 83 ms
13,000 KB
testcase_13 AC 49 ms
14,040 KB
testcase_14 AC 108 ms
12,464 KB
testcase_15 AC 61 ms
14,864 KB
testcase_16 AC 102 ms
13,256 KB
testcase_17 AC 27 ms
11,008 KB
testcase_18 AC 54 ms
14,552 KB
testcase_19 AC 173 ms
23,148 KB
testcase_20 AC 28 ms
7,976 KB
testcase_21 AC 71 ms
12,624 KB
testcase_22 AC 119 ms
13,048 KB
testcase_23 AC 119 ms
19,000 KB
testcase_24 AC 195 ms
25,376 KB
testcase_25 AC 196 ms
25,176 KB
testcase_26 AC 195 ms
25,516 KB
testcase_27 AC 193 ms
25,172 KB
testcase_28 AC 231 ms
25,340 KB
testcase_29 AC 222 ms
25,364 KB
testcase_30 AC 215 ms
25,192 KB
testcase_31 AC 222 ms
25,416 KB
testcase_32 AC 224 ms
25,364 KB
testcase_33 AC 208 ms
25,304 KB
testcase_34 AC 237 ms
17,912 KB
testcase_35 AC 231 ms
17,500 KB
testcase_36 AC 237 ms
17,544 KB
testcase_37 AC 256 ms
17,276 KB
testcase_38 AC 244 ms
17,480 KB
testcase_39 AC 221 ms
17,456 KB
testcase_40 AC 244 ms
17,412 KB
testcase_41 AC 228 ms
17,416 KB
testcase_42 AC 234 ms
17,460 KB
testcase_43 AC 224 ms
17,664 KB
testcase_44 AC 55 ms
20,740 KB
testcase_45 AC 57 ms
20,872 KB
testcase_46 AC 56 ms
20,972 KB
testcase_47 AC 55 ms
20,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <numeric>
#include <cmath>
#define ll long long
#define endl '\n'
using namespace std;
// category : 

int main() {
    ios_base::sync_with_stdio(false);
    
    int n,m; cin>>n>>m;

	vector<vector<ll>> dp1(2,vector<ll>(n+1,0));
	vector<vector<ll>> dp2(2,vector<ll>(n+1,0));

	vector<vector<bool>> flag1(2,vector<bool>(n+1,0));
	vector<vector<bool>> flag2(2,vector<bool>(n+1,0));

	flag1[0][1]=true;
	flag2[0][n]=true;
	
	int h[n+1]={}; for(int i=1;i<=n;i++) cin>>h[i];

	vector<set<int>> v(n+1);
	for(int i=0;i<m;i++){
		int x,y; cin>>x>>y;
		v[x].insert(y);
		v[y].insert(x);
	}

	for(int i=2;i<=n;i++){
		for(auto& j : v[i]){
			if(!(flag1[0][j]|flag1[1][j])) continue;
			
			if(h[i]>h[j]){
				if(flag1[0][j]){
					dp1[1][i]=max(dp1[1][i],dp1[0][j]+h[i]-h[j]);
					flag1[1][i]=true;
				}
			}
			else{
				dp1[0][i]=max({dp1[0][i],dp1[0][j],dp1[1][j]});
				flag1[0][i]=true;
			}
		}
	}
	if(flag1[0][n]|flag1[1][n])cout<<max(dp1[0][n],dp1[1][n])<<endl;
	else cout<<-1<<endl;

	for(int i=n-1;i>=1;i--){
		for(auto& j : v[i]){
			if(!(flag2[0][j]|flag2[1][j])) continue;
			
			if(h[i]>h[j]){
				if(flag2[0][j]){
					dp2[1][i]=max(dp2[1][i],dp2[0][j]+h[i]-h[j]);
					flag2[1][i]=true;
				}
			}
			else{
				dp2[0][i]=max({dp2[0][i],dp2[0][j],dp2[1][j]});
				flag2[0][i]=true;
			}
		}
	}
	if(flag2[0][1]|flag2[1][1]) cout<<max(dp2[0][1],dp2[1][1])<<endl;
	else cout<<-1<<endl;

	return 0;
}
0