結果
問題 | No.409 ダイエット |
ユーザー |
![]() |
提出日時 | 2018-09-26 18:09:03 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 103 ms / 2,000 ms |
コード長 | 2,827 bytes |
コンパイル時間 | 1,489 ms |
コンパイル使用メモリ | 164,980 KB |
実行使用メモリ | 11,716 KB |
最終ジャッジ日時 | 2024-10-11 19:22:44 |
合計ジャッジ時間 | 7,864 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 92 |
ソースコード
#include <bits/stdc++.h> #include <cstdio> #include <string> using namespace std; #define INF 100000000 #define YJ 1145141919 #define INF_INT_MAX 2147483647 #define INF_LL_MAX 9223372036854775807 #define EPS 1e-10 #define MOD 1000000007 #define Pi acos(-1) #define LL long long #define ULL unsigned long long #define LD long double #define int long long template<typename T> class ConvecHullTrick { private: // 直線群(配列) std::vector<std::pair<T, T>> lines; // 最小値(最大値)を求めるxが単調であるか bool isMonotonicX; // 最小/最大を判断する関数 std::function<bool(T l, T r)> comp; public: // コンストラクタ ( クエリが単調であった場合はflag = trueとする ) ConvecHullTrick(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, 0); }; // 直線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); } } }; const int MAX_N = 300005; int N, A, B, W; int D[MAX_N]; int dp[MAX_N]; signed main() { cin >> N >> A >> B >> W; for(int i = 0; i < N; i++) { cin >> D[i]; } dp[0] = 0; ConvecHullTrick<int> cht(true); for(int i = 1; i <= N; i++) { dp[i] = cht.get(i-1) + D[i-1] - A*(i-1) + (i * (i-1))/2 * B; //cerr << dp[i] << " " << cht.get(i-1) << " " << D[i-1] << endl; cht.add(-B*i, dp[i] + A*i + i*(i-1)/2*B); } int ans = 114514191981036436; for(int i = 0; i < N+1; i++) { //cerr << i << " " << dp[i] << endl; ans = min(ans, dp[i] + (-(N-i)*A + (N-i)*(N-i+1)/2*B)); } cout << ans+W << endl; return 0; }