結果

問題 No.409 ダイエット
ユーザー tossytossy
提出日時 2017-10-18 21:00:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 2,192 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 170,916 KB
実行使用メモリ 13,272 KB
最終ジャッジ日時 2023-09-08 17:35:34
合計ジャッジ時間 7,046 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 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 2 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,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 3 ms
4,384 KB
testcase_36 AC 3 ms
4,376 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 3 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 3 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 3 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 3 ms
4,376 KB
testcase_48 AC 3 ms
4,380 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 3 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 3 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 31 ms
7,476 KB
testcase_56 AC 54 ms
7,624 KB
testcase_57 AC 64 ms
12,196 KB
testcase_58 AC 28 ms
7,344 KB
testcase_59 AC 39 ms
7,980 KB
testcase_60 AC 26 ms
6,832 KB
testcase_61 AC 56 ms
12,024 KB
testcase_62 AC 59 ms
12,104 KB
testcase_63 AC 55 ms
12,496 KB
testcase_64 AC 30 ms
7,292 KB
testcase_65 AC 56 ms
9,220 KB
testcase_66 AC 62 ms
13,212 KB
testcase_67 AC 47 ms
10,872 KB
testcase_68 AC 39 ms
7,972 KB
testcase_69 AC 54 ms
13,024 KB
testcase_70 AC 79 ms
12,152 KB
testcase_71 AC 46 ms
7,184 KB
testcase_72 AC 94 ms
12,288 KB
testcase_73 AC 90 ms
13,272 KB
testcase_74 AC 57 ms
10,856 KB
testcase_75 AC 89 ms
12,396 KB
testcase_76 AC 65 ms
11,412 KB
testcase_77 AC 43 ms
6,148 KB
testcase_78 AC 51 ms
9,640 KB
testcase_79 AC 40 ms
6,884 KB
testcase_80 AC 91 ms
12,516 KB
testcase_81 AC 88 ms
11,912 KB
testcase_82 AC 66 ms
11,136 KB
testcase_83 AC 71 ms
10,828 KB
testcase_84 AC 59 ms
7,728 KB
testcase_85 AC 7 ms
4,380 KB
testcase_86 AC 56 ms
7,656 KB
testcase_87 AC 79 ms
11,628 KB
testcase_88 AC 33 ms
5,556 KB
testcase_89 AC 65 ms
6,880 KB
testcase_90 AC 30 ms
5,152 KB
testcase_91 AC 69 ms
7,044 KB
権限があれば一括ダウンロードができます

ソースコード

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

// Convec Hull Trick for min query
template<typename T, typename U=__int128>
class ConvexHullTrick {
private:
  vector<pair<T,T>> data;
  int pt;
  bool check(pair<T,T> &p){
    int s = data.size();
    return (U)(data[s-1].fi-data[s-2].fi)*(p.se-data[s-1].se) < (U)(data[s-1].se-data[s-2].se)*(p.fi-data[s-1].fi);
  }
public:
  ConvexHullTrick(){ pt = 0; }
  // 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){
    pt = min<int>(data.size()-1, pt);
    while(pt+1 < (int)data.size() && val(pt, x) >= val(pt+1, x)) pt++;
    return val(pt, 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