結果
| 問題 |
No.595 登山
|
| ユーザー |
qwewe
|
| 提出日時 | 2025-05-14 13:26:20 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,160 bytes |
| コンパイル時間 | 646 ms |
| コンパイル使用メモリ | 76,264 KB |
| 実行使用メモリ | 8,064 KB |
| 最終ジャッジ日時 | 2025-05-14 13:27:18 |
| 合計ジャッジ時間 | 2,430 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 4 WA * 21 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// A large enough value for infinity, considering N * max_H_diff or (N-1)*P
// Max N = 2e5, Max H_diff = 1e9 -> 2e14
// Max P = 1e9, (N-1)*P approx 2e5 * 1e9 = 2e14
const long long INF = 4e14 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
long long p;
cin >> n >> p;
vector<long long> h(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
}
// Constraints state N >= 2. If N=1 was allowed, cost is 0.
// if (n == 1) {
// cout << 0 << endl;
// return 0;
// }
// pre_walk_cost[i] = min cost to walk from location 0 to location i
vector<long long> pre_walk_cost(n);
pre_walk_cost[0] = 0;
for (int i = 0; i < n - 1; ++i) {
pre_walk_cost[i + 1] = pre_walk_cost[i] + max(0LL, h[i + 1] - h[i]);
}
// suf_walk_cost[i] = min cost to walk from location n-1 to location i (backwards)
vector<long long> suf_walk_cost(n);
suf_walk_cost[n - 1] = 0;
for (int i = n - 2; i >= 0; --i) {
suf_walk_cost[i] = suf_walk_cost[i + 1] + max(0LL, h[i] - h[i + 1]);
}
long long min_total_cost = INF;
// Strategy 1: 0 teleports, walk 0 -> n-1
// All locations {0, ..., n-1} are visited.
min_total_cost = pre_walk_cost[n - 1];
// Strategy 2: 1 teleport
// Walk 0..k, teleport from k to n-1, then walk (n-1)..(k+1).
// This covers locations {0, ..., k} and {k+1, ..., n-1}.
// k is the 0-indexed end location of the first segment.
for (int k = 0; k < n - 1; ++k) {
long long current_strategy_cost = pre_walk_cost[k] + p + suf_walk_cost[k + 1];
min_total_cost = min(min_total_cost, current_strategy_cost);
}
// Strategy 3: N-1 teleports (to visit N-1 new locations after starting at loc 0)
// Start at 0. Teleport to 1 (cost P), then from 1 to 2 (cost P), ..., then from n-2 to n-1 (cost P).
// This requires (n-1) teleports.
// (N-1)*P can be 0 if P=0.
min_total_cost = min(min_total_cost, (long long)(n - 1) * p);
cout << min_total_cost << endl;
return 0;
}
qwewe