結果
| 問題 | No.2100 [Cherry Alpha C] Two-way Steps |
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2022-10-18 08:38:23 |
| 言語 | C++17(clang) (clang++ 21.1.8 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 117 ms / 2,000 ms |
| コード長 | 1,527 bytes |
| 記録 | |
| コンパイル時間 | 2,544 ms |
| コンパイル使用メモリ | 148,096 KB |
| 実行使用メモリ | 11,520 KB |
| 最終ジャッジ日時 | 2026-03-17 18:47:02 |
| 合計ジャッジ時間 | 8,774 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 48 |
コンパイルメッセージ
main.cpp:27:17: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
27 | vector<int> E[N + 1];
| ^~~~~
main.cpp:27:17: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:19:7: note: declared here
19 | int N, M;
| ^
main.cpp:36:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
36 | ll dp[N + 1][2];
| ^~~~~
main.cpp:36:9: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:19:7: note: declared here
19 | int N, M;
| ^
2 warnings generated.
ソースコード
#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
int main() {
int N, M;
cin >> N >> M;
vector<ll> H(N + 1);
for (int i = 1; i <= N; ++i) {
cin >> H[i];
}
vector<int> E[N + 1];
for (int i = 0; i < M; ++i) {
int x, y;
cin >> x >> y;
E[x].push_back(y);
E[y].push_back(x);
}
ll dp[N + 1][2];
memset(dp, -1, sizeof(dp));
dp[1][0] = 0;
for (int i = 1; i < N; ++i) {
for (int k = 0; k < 2; ++k) {
if (dp[i][k] == -1) continue;
for (int j : E[i]) {
if (j < i) continue;
if (k == 1 && H[i] < H[j]) continue;
if (H[i] < H[j]) {
dp[j][1] = max(dp[j][1], dp[i][k] + abs(H[j] - H[i]));
} else {
dp[j][0] = max(dp[j][0], dp[i][k]);
}
}
}
}
cout << max(dp[N][0], dp[N][1]) << endl;
memset(dp, -1, sizeof(dp));
dp[N][0] = 0;
for (int i = N; i > 1; --i) {
for (int k = 0; k < 2; ++k) {
if (dp[i][k] == -1) continue;
for (int j : E[i]) {
if (i < j) continue;
if (k == 1 && H[j] > H[i]) continue;
if (H[i] < H[j]) {
dp[j][1] = max(dp[j][1], dp[i][k] + abs(H[j] - H[i]));
} else {
dp[j][0] = max(dp[j][0], dp[i][k]);
}
}
}
}
cout << max(dp[1][0], dp[1][1]) << endl;
return 0;
}
siman