結果

問題 No.409 ダイエット
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-03-23 12:01:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 3,015 bytes
コンパイル時間 1,643 ms
コンパイル使用メモリ 171,596 KB
実行使用メモリ 11,452 KB
最終ジャッジ日時 2023-09-08 17:21:21
合計ジャッジ時間 6,211 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,384 KB
testcase_55 AC 20 ms
6,616 KB
testcase_56 AC 31 ms
6,696 KB
testcase_57 AC 39 ms
10,832 KB
testcase_58 AC 17 ms
6,220 KB
testcase_59 AC 24 ms
7,044 KB
testcase_60 AC 16 ms
6,156 KB
testcase_61 AC 34 ms
10,188 KB
testcase_62 AC 36 ms
10,384 KB
testcase_63 AC 33 ms
10,040 KB
testcase_64 AC 19 ms
6,756 KB
testcase_65 AC 34 ms
8,032 KB
testcase_66 AC 38 ms
11,452 KB
testcase_67 AC 29 ms
10,116 KB
testcase_68 AC 24 ms
6,880 KB
testcase_69 AC 33 ms
10,796 KB
testcase_70 AC 37 ms
10,532 KB
testcase_71 AC 22 ms
6,600 KB
testcase_72 AC 45 ms
10,532 KB
testcase_73 AC 43 ms
10,908 KB
testcase_74 AC 28 ms
9,140 KB
testcase_75 AC 43 ms
10,504 KB
testcase_76 AC 32 ms
10,116 KB
testcase_77 AC 21 ms
5,660 KB
testcase_78 AC 25 ms
9,776 KB
testcase_79 AC 20 ms
6,504 KB
testcase_80 AC 44 ms
10,312 KB
testcase_81 AC 42 ms
10,828 KB
testcase_82 AC 32 ms
9,952 KB
testcase_83 AC 35 ms
11,208 KB
testcase_84 AC 28 ms
7,080 KB
testcase_85 AC 5 ms
4,380 KB
testcase_86 AC 27 ms
6,896 KB
testcase_87 AC 37 ms
10,296 KB
testcase_88 AC 16 ms
5,196 KB
testcase_89 AC 30 ms
6,200 KB
testcase_90 AC 15 ms
4,772 KB
testcase_91 AC 31 ms
6,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)




#define INF 1LL<<60
template<typename T>
class CHT {
private:
	// 直線群(配列)
	std::vector<std::pair<T, T>> lines;
	// 最小値(最大値)を求めるxが単調であるか
	bool isMonotonicX;
	// 最小/最大を判断する関数
	std::function<bool(T l, T r)> comp;

public:
	// コンストラクタ ( クエリが単調であった場合はflag = trueとする )
	CHT(bool flagX = false, std::function<bool(T l, T r)> compFunc = [](T l, T r) {return l >= r; })
		:isMonotonicX(flagX), comp(compFunc) {
		//lines.emplace_back(0, INF);
	};

	// 直線l1, l2, l3のうちl2が不必要であるかどうか
	bool check(std::pair<T, T> l1, std::pair<T, T> l2, std::pair<T, T> l3) {
		if (l1 < l3) std::swap(l1, l3);
		return (l3.second - l2.second) * (l2.first - l1.first) >= (l2.second - l1.second) * (l3.first - l2.first);
	}

	// 直線y=ax+bを追加する
	void add(T a, T b) {
		std::pair<T, T> line(a, b);
		while (lines.size() >= 2 && check(*(lines.end() - 2), lines.back(), line))
			lines.pop_back();
		lines.emplace_back(line);
	}

	// i番目の直線f_i(x)に対するxの時の値を返す
	T f(int i, T x) {
		return lines[i].first * x + lines[i].second;
	}

	// i番目の直線f_i(x)に対するxの時の値を返す
	T f(std::pair<T, T> line, T x) {
		return line.first * x + line.second;
	}

	// 直線群の中でxの時に最小(最大)となる値を返す
	T get(T x) {
		// 最小値(最大値)クエリにおけるxが単調
		if (isMonotonicX) {
			static int head = 0;
			while (lines.size() - head >= 2 && comp(f(head, x), f(head + 1, x)))
				++head;
			return f(head, x);
		}
		else {
			int low = -1, high = lines.size() - 1;
			while (high - low > 1) {
				int mid = (high + low) / 2;
				(comp(f(mid, x), f(mid + 1, x)) ? low : high) = mid;
			}
			return f(high, x);
		}
	}
};
//-----------------------------------------------------------------------------------
typedef long long ll;
int N, A, B, W;
int D[301010];
//-----------------------------------------------------------------------------------
ll dp[301010];
int main() {
	cin >> N >> A >> B >> W;
	rep(i, 0, N) scanf("%d", &D[i]);

	/* Naive
	dp[0] = W;
	rep(i, 1, N + 1) {
		dp[i] = INF;
		rep(j, 0, i) {
			ll w = dp[j];
			w -= 1LL * A * (i - j - 1);
			w += 1LL * (i - j - 1) * (i - j) / 2 * B;
			dp[i] = min(dp[i], w);
		}
		dp[i] += D[i - 1];
	}
	//*/

	//rep(i, 0, N + 1) printf("dp[%d] = %d\n", i, dp[i]);

	//* Optimized
	CHT<ll> cht(true);
	dp[0] = W;
	cht.add(0, W);
	rep(i, 1, N + 1) {
		//cout << cht.get(i) << endl;
		dp[i] = cht.get(i) - 1LL * (i - 1) * A + (1LL * (i - 1) * i) / 2 * B + D[i - 1];
		cht.add(-1LL * B * i, dp[i] + 1LL * A * i + (1LL * i * i + i) / 2 * B);
	}
	//*/

	//rep(i, 0, N + 1) printf("dp[%d] = %d\n", i, dp[i]);

	ll ans = INF;
	rep(i, 0, N + 1) {
		ll w = dp[i];
		int n = N - i;
		w -= 1LL * A * n;
		w += 1LL * n * (n + 1) / 2 * B;
		ans = min(ans, w);
	}

	cout << ans << endl;
}
0