結果
| 問題 |
No.2100 [Cherry Alpha C] Two-way Steps
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-10-14 22:31:36 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 98 ms / 2,000 ms |
| コード長 | 1,545 bytes |
| コンパイル時間 | 2,192 ms |
| コンパイル使用メモリ | 200,784 KB |
| 最終ジャッジ日時 | 2025-02-08 04:21:49 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 48 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
V<ll> H(N);
rep(i, N) cin >> H[i];
V<V<int>> G1(N), G2(N);
rep(i, M) {
int x, y;
cin >> x >> y;
x--, y--;
G1[x].push_back(y);
G2[N - 1 - y].push_back(N - 1 - x);
}
const ll INF = 1LL << 60;
auto calc = [&INF](V<V<int>> &G, V<ll> &H) -> ll {
int N = (int)G.size();
V<ll> dp0(N, -INF), dp1(N, -INF);
dp0[0] = dp1[0] = 0;
for (int i = 0; i < N; i++) {
if (dp0[i] != -INF) {
for (auto nex : G[i]) {
if (H[i] < H[nex]) {
dp1[nex] = max(dp1[nex], dp0[i] + H[nex] - H[i]);
} else {
dp0[nex] = max(dp0[nex], dp0[i]);
}
}
}
if (dp1[i] != -INF) {
for (auto nex : G[i]) {
if (H[i] > H[nex]) {
dp0[nex] = max(dp0[nex], dp1[i]);
}
}
}
}
return max({dp0[N - 1], dp1[N - 1], -1LL});
};
ll ans1 = calc(G1, H);
reverse(H.begin(), H.end());
ll ans2 = calc(G2, H);
cout << ans1 << '\n' << ans2 << '\n';
return 0;
}