結果
問題 | No.595 登山 |
ユーザー |
![]() |
提出日時 | 2020-07-13 16:17:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 113 ms / 1,500 ms |
コード長 | 2,079 bytes |
コンパイル時間 | 2,074 ms |
コンパイル使用メモリ | 197,676 KB |
最終ジャッジ日時 | 2025-01-11 20:16:56 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 25 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,n) REP(i,0,n) #define REP(i,s,e) for(int i=(s); i<(int)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--) #define all(r) r.begin(),r.end() #define rall(r) r.rbegin(),r.rend() typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; const ll INF = 1e18; const ll MOD = 1e9 + 7; template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);} template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);} #define DEBUG_MODE #ifdef DEBUG_MODE #define dump(x) cout << #x << " : " << x << " " #define dumpL(x) cout << #x << " : " << x << '\n' #define LINE cout << "line : " << __LINE__ << " " #define LINEL cout << "line : " << __LINE__ << '\n' #define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << " " #define dumpVL(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl #define STOP assert(false) #else #define dump(x) #define dumpL(x) #define LINE #define LINEL #define dumpV(v) #define dumpVL(v) #define STOP assert(false) #endif #define mp make_pair namespace std { template<class S, class T> ostream &operator <<(ostream& out, const pair<S, T>& a) { out << '(' << a.fi << ", " << a.se << ')'; return out; } } int main(){ ll n, p; cin >> n >> p; vl h(n); rep(i, n) cin >> h[i]; vl left(n); iota(all(left), 0); REP(i, 1, n) if(h[i-1] <= h[i]) left[i] = left[i-1]; vector<vl> dp(n, vl(2,INF)); dp[0][0] = 0LL; REP(i, 1, n) { chmin(dp[i][0], dp[i-1][0]+min(p, max(0LL, h[i]- h[i-1]))); chmin(dp[i][0], dp[i-1][1]+p); chmin(dp[i][1], dp[i-1][1]+min(p, max(0LL, h[i-1]- h[i]))); { int j = left[i]-1; if(j-1<0) j = 0; chmin(dp[i][1], min(dp[j][0], dp[j][1])+p); } } cout << min(dp.back().front(), dp.back().back()) << '\n'; // rep(i, n) { // cout << " " << dp[i][0] << " " << dp[i][1] << '\n'; // } return 0; }