結果

問題 No.409 ダイエット
ユーザー snrnsidysnrnsidy
提出日時 2021-06-15 00:28:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,658 bytes
コンパイル時間 2,251 ms
コンパイル使用メモリ 205,248 KB
実行使用メモリ 19,192 KB
最終ジャッジ日時 2023-08-26 14:38:51
合計ジャッジ時間 9,440 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,044 KB
testcase_01 AC 4 ms
8,200 KB
testcase_02 AC 4 ms
8,240 KB
testcase_03 AC 4 ms
8,056 KB
testcase_04 AC 4 ms
8,064 KB
testcase_05 AC 4 ms
8,064 KB
testcase_06 AC 4 ms
8,108 KB
testcase_07 AC 4 ms
8,064 KB
testcase_08 AC 4 ms
8,120 KB
testcase_09 AC 4 ms
8,128 KB
testcase_10 AC 4 ms
8,052 KB
testcase_11 AC 5 ms
8,060 KB
testcase_12 AC 4 ms
8,076 KB
testcase_13 AC 4 ms
8,124 KB
testcase_14 AC 4 ms
8,108 KB
testcase_15 AC 4 ms
8,052 KB
testcase_16 AC 4 ms
8,108 KB
testcase_17 AC 4 ms
8,064 KB
testcase_18 AC 4 ms
8,112 KB
testcase_19 AC 4 ms
8,364 KB
testcase_20 AC 4 ms
8,068 KB
testcase_21 AC 4 ms
8,116 KB
testcase_22 AC 4 ms
8,072 KB
testcase_23 AC 4 ms
8,196 KB
testcase_24 AC 4 ms
8,072 KB
testcase_25 AC 4 ms
8,072 KB
testcase_26 AC 4 ms
8,368 KB
testcase_27 AC 4 ms
8,200 KB
testcase_28 AC 4 ms
8,048 KB
testcase_29 AC 4 ms
8,068 KB
testcase_30 AC 5 ms
8,120 KB
testcase_31 AC 4 ms
8,264 KB
testcase_32 AC 4 ms
8,056 KB
testcase_33 AC 4 ms
8,116 KB
testcase_34 AC 4 ms
8,068 KB
testcase_35 AC 4 ms
8,152 KB
testcase_36 AC 4 ms
8,212 KB
testcase_37 AC 4 ms
8,324 KB
testcase_38 AC 4 ms
8,188 KB
testcase_39 AC 5 ms
8,156 KB
testcase_40 AC 5 ms
8,264 KB
testcase_41 AC 5 ms
8,260 KB
testcase_42 AC 5 ms
8,196 KB
testcase_43 AC 4 ms
8,396 KB
testcase_44 AC 5 ms
8,244 KB
testcase_45 AC 5 ms
8,264 KB
testcase_46 AC 4 ms
8,160 KB
testcase_47 AC 5 ms
8,304 KB
testcase_48 AC 4 ms
8,172 KB
testcase_49 AC 5 ms
8,468 KB
testcase_50 AC 4 ms
8,288 KB
testcase_51 AC 4 ms
8,188 KB
testcase_52 AC 5 ms
8,248 KB
testcase_53 AC 5 ms
8,156 KB
testcase_54 AC 4 ms
8,132 KB
testcase_55 AC 27 ms
11,928 KB
testcase_56 AC 34 ms
10,532 KB
testcase_57 AC 52 ms
19,192 KB
testcase_58 AC 24 ms
10,876 KB
testcase_59 AC 33 ms
12,272 KB
testcase_60 AC 22 ms
10,752 KB
testcase_61 AC 45 ms
14,540 KB
testcase_62 AC 49 ms
17,880 KB
testcase_63 AC 45 ms
14,372 KB
testcase_64 AC 27 ms
12,120 KB
testcase_65 AC 48 ms
15,000 KB
testcase_66 AC 51 ms
18,688 KB
testcase_67 AC 38 ms
14,340 KB
testcase_68 AC 33 ms
12,244 KB
testcase_69 AC 45 ms
13,996 KB
testcase_70 AC 50 ms
14,084 KB
testcase_71 AC 29 ms
10,912 KB
testcase_72 AC 59 ms
17,392 KB
testcase_73 AC 56 ms
14,492 KB
testcase_74 AC 36 ms
14,256 KB
testcase_75 AC 56 ms
17,208 KB
testcase_76 AC 41 ms
14,744 KB
testcase_77 AC 28 ms
10,300 KB
testcase_78 AC 33 ms
14,204 KB
testcase_79 AC 26 ms
10,680 KB
testcase_80 AC 56 ms
17,240 KB
testcase_81 AC 55 ms
13,884 KB
testcase_82 AC 41 ms
13,876 KB
testcase_83 AC 44 ms
14,336 KB
testcase_84 AC 37 ms
12,136 KB
testcase_85 AC 8 ms
8,584 KB
testcase_86 AC 37 ms
12,116 KB
testcase_87 AC 49 ms
14,808 KB
testcase_88 AC 22 ms
10,152 KB
testcase_89 AC 39 ms
10,344 KB
testcase_90 AC 21 ms
9,240 KB
testcase_91 AC 41 ms
10,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long lint;
typedef long double llf;
typedef pair<lint, lint> pi;

struct line
{
	long long int a, b;
};

struct cht {
	vector<pi> v;
	int p;
	double cross(int a, int b)
	{
		return (1.0 * (v[a].second - v[b].second)) / (v[b].first - v[a].first);
	}
	lint query(long long int x) {
		if (v.empty()) return 1e18;
		while (p + 1 < v.size() && v[p].first * x + v[p].second > v[p + 1].first * x + v[p + 1].second) p++;
		return v[p].first * x + v[p].second;
	}
	lint query2(long long int x)
	{
		int l = 0;
		int r = v.size() - 1;
		while (l != r)
		{
			int m = (l + r) / 2;
			if (cross(m, m + 1) <= x)
			{
				l = m + 1;
			}
			else
			{
				r = m;
			}
		}
		return v[l].first * x + v[l].second;
	}
	bool chk(pi a, pi b, pi c) {
		return 1ll * (b.first - a.first) * (b.second - c.second) < (c.first - b.first) * (a.second - b.second);
	}
	void add(int x, lint y) {
		if (!v.empty() && v.back().first == x) {
			if (v.back().second > y) return;
			v.pop_back();
		}
		while (v.size() >= p + 2 && chk(v[v.size() - 2], v.back(), pi(x, y))) {
			v.pop_back();
		}
		v.emplace_back(x, y);
	}
}cht;

long long int dp[2][300005];

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	
	long long int N, A, W, B, t;
	vector <long long int> D;

	cin >> N >> A >> B >> W;
	for (int i = 0; i < N; i++)
	{
		cin >> t;
		D.push_back(t);
	}

	fill(dp[0], dp[0] + 300005, 1e18);
	fill(dp[1], dp[1] + 300005, 1e18);
	dp[1][0] = W;

	/*
	for (int i = 1; i <= N; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			dp[1][i] = min(dp[1][i], dp[j][i - 1] + D[i - 1]);
		}
		for (int j = 0; j < i; j++)
		{
			long long int n = i - j;
			long long int sum = n * (n + 1) / 2;
			sum *= B;
			sum -= (A * n);
			dp[0][i] = min(dp[0][i], dp[1][j] + sum);
		}
	}
	*/

	//dp[0][x] = min(0<=y<x) dp[1][y] + B*(x-y)*(x-y+1)/2 - A*(x-y)
	//dp[1][y] + B/2*(x^2-xy+x-xy+y^2-y) - A*(x-y)
	//dp[1][y] - B*x*y + (B/2)(x^2 + y^2 + x - y) - A * (x-y)
	//dp[1][y] - Bxy + (B/2)(x^2 + x) - Ax + (B/2)(y^2 - y) + A*y
	// A[x] = -Bx , B[y] = y , C[y] = dp[1][y] + (B/2)(y^2 - y) + Ay , D[x] = (B/2)(x^2 + x) - Ax

	line ins;
	ins.a = 0;
	ins.b = dp[1][0];
	cht.add(ins.a, ins.b);
	for (long long int i = 1; i <= N; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			dp[1][i] = min(dp[1][i], dp[j][i - 1] + D[i - 1]);
		}
		//cout << cht.query2(B*i) + (B * ((i * (i + 1)) / 2)) - (A * i) << '\n';
		dp[0][i] = min(dp[0][i], cht.query2(B*i) + (B * ((i * (i + 1)) / 2)) - (A * i));
		line ins;
		ins.a = -i;
		ins.b = dp[1][i] + (B * ((i*(i-1)) / 2)) + A * i;
		cht.add(ins.a, ins.b);
	}

	cout << min(dp[0][N],dp[1][N]) << '\n';

	return 0;
}
0