結果
| 問題 |
No.2100 [Cherry Alpha C] Two-way Steps
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-07 11:03:11 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 265 ms / 2,000 ms |
| コード長 | 1,414 bytes |
| コンパイル時間 | 10,284 ms |
| コンパイル使用メモリ | 337,628 KB |
| 実行使用メモリ | 32,940 KB |
| 最終ジャッジ日時 | 2024-06-26 13:27:05 |
| 合計ジャッジ時間 | 18,293 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 48 |
ソースコード
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
using pi=pair<int,int>;
int n,m;
vector<int> h;
vector<pi> xy;
void reverseProblem(){
reverse(h.begin(),h.end());
for(int i=0;i<m;i++){
swap(xy[i].first,xy[i].second);
xy[i].first=n-1-xy[i].first;
xy[i].second=n-1-xy[i].second;
}
}
long long solve(){
vector<vector<long long>> dp(n,vector<long long>(2,-8e18));
dp[0][0]=0;
dp[0][1]=0;
vector<vector<int>> g(n);
for(int i=0;i<m;i++){
g[xy[i].first].push_back(xy[i].second);
}
for(int i=0;i<n;i++){
for(auto &nx : g[i]){
if(h[i]<h[nx]){dp[nx][1]=max(dp[nx][1],dp[i][0]+h[nx]-h[i]);}
else{dp[nx][0]=max(dp[nx][0],max(dp[i][0],dp[i][1]));}
}
}
long long res=max(dp[n-1][0],dp[n-1][1]);
if(res<0){res=-1;}
return res;
}
int main(){
registerValidation();
n=inf.readInt(2,100000);inf.readSpace();
m=inf.readInt(1,150000);inf.readEoln();
h=inf.readInts(n,0,1000000000);inf.readEoln();
xy.resize(m);
set<pi> st;
for(int i=0;i<m;i++){
xy[i].first=inf.readInt(1,n);inf.readSpace();
xy[i].second=inf.readInt(xy[i].first+1,n);inf.readEoln();
xy[i].first--;xy[i].second--;
st.insert(xy[i]);
ensuref(h[xy[i].first]!=h[xy[i].second],"This is not a stairs");
}
ensuref(st.size()==m,"doubled stairs");
inf.readEof();
cout << solve() << "\n";
reverseProblem();
cout << solve() << "\n";
return 0;
}