結果

問題 No.595 登山
ユーザー ふっぴーふっぴー
提出日時 2017-11-11 00:19:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,169 bytes
コンパイル時間 1,868 ms
コンパイル使用メモリ 174,768 KB
実行使用メモリ 18,348 KB
最終ジャッジ日時 2024-05-03 15:03:48
合計ジャッジ時間 5,747 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 WA -
testcase_02 RE -
testcase_03 RE -
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
const int inf = 1000000001;
const ll INF = 1e18 * 4;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout<<setprecision(15)<<fixed<<p<<endl;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };

class mountain {
public:
	int left, top, right;
};

int main() {
	int n, i, j;
	ll p;
	cin >> n >> p;
	vl h(n + 1);
	for (i = 1; i <= n; i++) {
		cin >> h[i];
	}
	vector<mountain> vm;
	int left = 0, right = 1;
	int now = 1;
	while (right < n) {
		mountain tmp;
		tmp.left = left;
		while (h[left] <= h[now] && h[now] <= h[now + 1]) {
			now++;
			if (now == n) {
				tmp.top = now - 1;
				tmp.right = now;
				break;
			}
		}
		tmp.top = now;
		now++;
		if (now == n) {
			tmp.right = now - 1;
			break;
		}
		while (h[left] >= h[now] && h[now] >= h[now + 1]) {
			now++;
			if (now == n) {
				tmp.right = now;
				break;
			}
		}
		tmp.right = now;
		vm.push_back(tmp);
		left = right;
	}
	int m = vm.size();
	vll cost(m, vl(3));
	for (i = 0; i < m; i++) {
		ll up = 0;
		for (j = vm[i].left; j < vm[i].top; j++) {
			up += min(p, h[j + 1] - h[j]);
		}
		cost[i][0] = up;
		up = 0;
		for (j = vm[i].right; j > vm[i].top; j--) {
			up += min(p, h[j - 1] - h[j]);
		}
		cost[i][1] = up;
		cost[i][2] = p * 2;
	}
	vll dp(m + 1,vl(2, INF));
	dp[0][0] = 0;
	for (i = 0; i < m; i++) {
		dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + min(cost[i][0], cost[i][2]));
		dp[i + 1][0] = min(dp[i + 1][0], dp[i + 1][1] + min(cost[i][2], p + cost[i][0]));
		dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + cost[i][1]);
		dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + p + min(p, cost[i][1]));
	}
	cout << dp[m][0] + dp[m][1] << endl;
}
0