結果

問題 No.409 ダイエット
ユーザー tossy
提出日時 2017-10-19 00:45:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 1,676 ms
コンパイル使用メモリ 174,824 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2024-06-26 10:24:28
合計ジャッジ時間 6,306 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 11200000001234567LL
#define long long long // for codeforces

// Convex Hull Trick for min query
template<typename T, typename U=__int128>
class ConvexHullTrick {
private:
  deque<pair<T,T>> data;
  bool check(const pair<T,T> &p3){
    auto &p1 = data[data.size()-2];
    auto &p2 = data[data.size()-1];
    return (U)(p2.fi-p1.fi)*(p3.se-p2.se) < (U)(p2.se-p1.se)*(p3.fi-p2.fi);
  }
public:
  ConvexHullTrick(){ }
  // additions of 'a' shold be NON-INCREASING
  void add(T a, T b){
    auto p = mp(a,b);
    while(data.size()>=2 && !check(p)) data.pop_back();
    data.push_back(p);
  }
  inline T val(int i, T x) {
    return data[i].fi*x + data[i].se;
  }
  // queries shold be NON-DECREASING
  T query(T x){
    while(data.size()>=2 && val(0, x) >= val(1, x)) data.pop_front();
    return val(0, x);
  }
};

int main(){
  long n,a,b,w;
  cin>>n>>a>>b>>w;
  vector<long> d(n);
  rep(i,n) cin>>d[i];

  vector<long> dp(n+1, 0);
  dp[0] = w;
  ConvexHullTrick<long> cht;
  cht.add(b,w);
  rep(i,n){
    dp[i+1] = cht.query(i) - i*a + b*i*(i-1)/2 + d[i];
    cht.add(-b*i, dp[i+1] + (i+1)*a + b*i*(i+1)/2);
  }

  long ans = dp[n];
  rep(i,n) ans = min(ans, dp[i] -a*(n-i) + b*(n-i)*(n-i+1)/2);

  cout << ans << endl;

  return 0;
}
0