結果

問題 No.409 ダイエット
ユーザー tossytossy
提出日時 2017-10-19 00:45:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 1,873 ms
コンパイル使用メモリ 173,132 KB
実行使用メモリ 7,692 KB
最終ジャッジ日時 2023-09-08 17:35:55
合計ジャッジ時間 6,824 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 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,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,384 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,384 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,384 KB
testcase_45 AC 3 ms
4,380 KB
testcase_46 AC 3 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 3 ms
4,384 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 3 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 30 ms
5,144 KB
testcase_56 AC 56 ms
7,556 KB
testcase_57 AC 60 ms
7,560 KB
testcase_58 AC 26 ms
4,868 KB
testcase_59 AC 37 ms
5,812 KB
testcase_60 AC 23 ms
4,544 KB
testcase_61 AC 53 ms
6,920 KB
testcase_62 AC 55 ms
7,552 KB
testcase_63 AC 52 ms
7,092 KB
testcase_64 AC 28 ms
5,176 KB
testcase_65 AC 56 ms
7,240 KB
testcase_66 AC 59 ms
7,692 KB
testcase_67 AC 44 ms
6,640 KB
testcase_68 AC 38 ms
5,728 KB
testcase_69 AC 52 ms
7,032 KB
testcase_70 AC 76 ms
6,768 KB
testcase_71 AC 43 ms
5,436 KB
testcase_72 AC 92 ms
7,396 KB
testcase_73 AC 88 ms
6,984 KB
testcase_74 AC 55 ms
5,608 KB
testcase_75 AC 87 ms
6,976 KB
testcase_76 AC 62 ms
5,932 KB
testcase_77 AC 43 ms
4,868 KB
testcase_78 AC 48 ms
5,536 KB
testcase_79 AC 38 ms
4,980 KB
testcase_80 AC 88 ms
7,392 KB
testcase_81 AC 86 ms
7,024 KB
testcase_82 AC 63 ms
5,972 KB
testcase_83 AC 69 ms
6,200 KB
testcase_84 AC 56 ms
5,916 KB
testcase_85 AC 7 ms
4,380 KB
testcase_86 AC 55 ms
5,776 KB
testcase_87 AC 76 ms
6,840 KB
testcase_88 AC 32 ms
4,380 KB
testcase_89 AC 65 ms
6,188 KB
testcase_90 AC 31 ms
4,448 KB
testcase_91 AC 71 ms
6,300 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

// 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