結果

問題 No.409 ダイエット
ユーザー hashiryohashiryo
提出日時 2019-05-08 20:37:02
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 2,056 bytes
コンパイル時間 875 ms
コンパイル使用メモリ 100,952 KB
実行使用メモリ 8,944 KB
最終ジャッジ日時 2024-07-02 00:31:41
合計ジャッジ時間 5,514 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <float.h>
#include <random>

#define repeat(i,n) for (int i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'
#define debugArrayP(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge].first<< " " << x[hoge].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const int INF = INT_MAX;
const ll MOD = 1e9+7;


template<typename T>
class ConvexHullTrick {
private:
    vector<pair<T, T> > lines;
    int head;
    bool check(pair<T, T> l1, pair<T, T> l2, pair<T, T> l3) {
        return (l3.second - l2.second) * (l2.first - l1.first) >= (l2.second - l1.second) * (l3.first - l2.first);
    }

    T f(int i, T x) {
        return lines[i].first * x + lines[i].second;
    }
public:
    ConvexHullTrick():head(0){}
    void insert(T a, T b) {
        pair<T, T> line(a, b);
        while (lines.size()-head >= 2 && check(*(lines.end() - 2), lines.back(), line))
            lines.pop_back();
        lines.emplace_back(line);
    }
    T getmin(T x) {
        while (lines.size() - head >= 2 && (f(head, x)>=f(head + 1, x)))
            ++head;
        return f(head, x);
    }
};


int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  ll A,B,W;
  cin>>N>>A>>B>>W;
  ConvexHullTrick<ll> CHT;
  CHT.insert(-B,2*W);
  for(ll i=1;i<=N;i++){
    ll D;cin>>D;
    ll dp=(CHT.getmin(i)+B*i*i-2*A*(i-1)+2*D)/2;
    CHT.insert(-B*(1+2*i),B*(i+1)*i+2*A*i+2*dp);
  }
  cout<<(CHT.getmin(N+1)+B*(N+1)*(N+1)-2*A*(N))/2<<endl;
  return 0;
}
0