結果

問題 No.409 ダイエット
ユーザー SHIJOU
提出日時 2020-10-10 12:02:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 2,069 bytes
コンパイル時間 2,258 ms
コンパイル使用メモリ 202,308 KB
最終ジャッジ日時 2025-01-15 06:16:38
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}



//head



int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, a, b, w;
	cin >> n >> a >> b >> w;
	a <<= 1;
	b <<= 1;
	vi d(n);
	rep(i, n) cin >> d[i];
	deque<pair<ll, ll>> dq;
	auto push = [&](ll nowtime, ll noww)->pair<ll, ll> {
		return {-a - nowtime*b + (b>>1), a*nowtime + nowtime*(nowtime-1)*(b>>1) + noww};
	};
	auto val = [&](int i, ll nt) {
		auto [u, v] = dq[i];
		return (b>>1)*nt*nt + u*nt + v;
	};
	auto check = [&](pair<ll, ll> &a, pair<ll, ll> &b, pair<ll, ll> &c) {
		return (c.second-b.second) * (b.first-a.first) < (b.second-a.second) * (c.first-b.first);
	};
	dq.emplace_back(push(0, w<<1));
	rep(i, n) {
		while(dq.size() > 1 and val(0, i) >= val(1, i)) dq.pop_front();
		ll now = val(0, i) + (d[i]<<1);
		auto p = push(i+1, now);
		while(dq.size() > 1 and !check(dq[dq.size()-2], dq.back(), p)) dq.pop_back();
		dq.emplace_back(p);
	}
	while(dq.size() > 1 and val(0, n) > val(1, n)) dq.pop_front();
	cout << val(0, n)/2 << endl;
}


// o + ai - an + b(n-i)(n-i+1)/2
// o + n((b/2)n - a - ib + b/2) + ai + i(i-1)b/2
// (b/2)n^2 + (-a - ib + b/2)n + ai + i(i-1)b/2 + o
0