結果

問題 No.409 ダイエット
ユーザー ttkkggwwttkkggww
提出日時 2023-02-11 19:24:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 2,895 bytes
コンパイル時間 5,953 ms
コンパイル使用メモリ 264,268 KB
実行使用メモリ 11,012 KB
最終ジャッジ日時 2023-09-22 15:17:21
合計ジャッジ時間 9,770 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 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 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 3 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 3 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,380 KB
testcase_55 AC 32 ms
6,784 KB
testcase_56 AC 26 ms
7,508 KB
testcase_57 AC 65 ms
10,952 KB
testcase_58 AC 28 ms
6,516 KB
testcase_59 AC 39 ms
7,328 KB
testcase_60 AC 25 ms
6,416 KB
testcase_61 AC 57 ms
9,872 KB
testcase_62 AC 59 ms
10,612 KB
testcase_63 AC 57 ms
9,696 KB
testcase_64 AC 30 ms
6,720 KB
testcase_65 AC 60 ms
8,828 KB
testcase_66 AC 64 ms
10,404 KB
testcase_67 AC 49 ms
9,096 KB
testcase_68 AC 38 ms
7,328 KB
testcase_69 AC 56 ms
9,340 KB
testcase_70 AC 61 ms
9,356 KB
testcase_71 AC 33 ms
6,452 KB
testcase_72 AC 73 ms
11,012 KB
testcase_73 AC 67 ms
9,284 KB
testcase_74 AC 41 ms
7,492 KB
testcase_75 AC 68 ms
9,888 KB
testcase_76 AC 47 ms
8,388 KB
testcase_77 AC 32 ms
5,912 KB
testcase_78 AC 35 ms
7,472 KB
testcase_79 AC 29 ms
6,180 KB
testcase_80 AC 68 ms
10,484 KB
testcase_81 AC 66 ms
9,436 KB
testcase_82 AC 49 ms
8,344 KB
testcase_83 AC 53 ms
8,628 KB
testcase_84 AC 42 ms
7,100 KB
testcase_85 AC 6 ms
4,380 KB
testcase_86 AC 42 ms
7,028 KB
testcase_87 AC 60 ms
9,420 KB
testcase_88 AC 24 ms
5,400 KB
testcase_89 AC 49 ms
6,644 KB
testcase_90 AC 23 ms
4,932 KB
testcase_91 AC 52 ms
6,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
ll n,A,B,w;
vector<ll> d;
/**
 * @brief Convex Hull Trick Add Monotone
 * @docs docs/convex-hull-trick-add-monotone.md
*/
template< typename T, bool isMin >
struct ConvexHullTrickAddMonotone {/*{{{*/
#define F first
#define S second
	using P = pair< T, T >;
	deque< P > H;

	ConvexHullTrickAddMonotone() = default;

	bool empty() const { return H.empty(); }

	void clear() { H.clear(); }

	inline ll sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); }

	inline bool check(const P &a, const P &b, const P &c) {
		if(b.S == a.S || c.S == b.S)
			return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S);
		//return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F);
		if(is_integral< T >::value) {
			return (b.S - a.S) / (a.F - b.F) >= (c.S - b.S) / (b.F - c.F);
		} else {
			return
				(b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >=
				(c.F - b.F) * sgn(b.S - a.S) / abs(c.S - b.S);
		}
	}

	void add(T a, T b) {
		if(!isMin) a *= -1, b *= -1;
		P line(a, b);
		if(empty()) {
			H.emplace_front(line);
			return;
		}
		if(H.front().F <= a) {
			if(H.front().F == a) {
				if(H.front().S <= b) return;
				H.pop_front();
			}
			while(H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front();
			H.emplace_front(line);
		} else {
			assert(a <= H.back().F);
			if(H.back().F == a) {
				if(H.back().S <= b) return;
				H.pop_back();
			}
			while(H.size() >= 2 && check(H[H.size() - 2], H.back(), line)) H.pop_back();
			H.emplace_back(line);
		}
	}

	inline T get_y(const P &a, const T &x) {
		return a.F * x + a.S;
	}

	T query(T x) {
		assert(!empty());
		ll l = -1, r = H.size() - 1;
		while(l + 1 < r) {
			ll m = (l + r) >> 1;
			if(get_y(H[m], x) >= get_y(H[m + 1], x)) l = m;
			else r = m;
		}
		if(isMin) return get_y(H[r], x);
		return -get_y(H[r], x);
	}

	T query_monotone_inc(T x) {
		assert(!empty());
		while(H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x)) H.pop_front();
		if(isMin) return get_y(H.front(), x);
		return -get_y(H.front(), x);
	}

	T query_monotone_dec(T x) {
		assert(!empty());
		while(H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x)) H.pop_back();
		if(isMin) return get_y(H.back(), x);
		return -get_y(H.back(), x);
	}

#undef F
#undef S
};/*}}}*/
void solve(){
	vector<ll> dp(n+1,INT_MAX);
	ConvexHullTrickAddMonotone<ll,true> cht;
		dp[0] = w;
	for(ll i = 0;i<=n;i++){
		//if(i==1)continue;
		if(i){
			i--;
			ll x = (i*i+i)/2*B-A*i;
			ll cur = cht.query(i)+x;
			i++;
			//if(i==1)cout<<x<<endl;
			dp[i] = cur+d[i-1];
		}
		cht.add(-B*i,dp[i]+(i*i-i)/2*B+A*i);
		//cout<<cht.query(1)<<endl;
	}
	ll x = (n*n+n)/2*B-A*n;
	cout<<cht.query(n)+x<<endl;
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> n >> A >> B >> w;
	d = vector<ll>(n);
	for(auto &i:d)cin >> i;
	solve();
}
0