結果

問題 No.409 ダイエット
ユーザー goodbatongoodbaton
提出日時 2017-02-13 10:39:04
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,284 bytes
コンパイル時間 605 ms
コンパイル使用メモリ 81,160 KB
実行使用メモリ 9,436 KB
最終ジャッジ日時 2024-06-26 10:09:50
合計ジャッジ時間 4,268 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:53:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   53 |   scanf("%d%lld%lld%lld",&n,&a,&b,&w);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:56:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   56 |     scanf("%lld",d+i);
      |     ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 300010

int n;
ll a,b,w;
ll d[SIZE];
ll dp[SIZE];

ll f_a(int t){
  return - b * t;
}

ll f_b(int t){
  return dp[t] + a * t + b * ((ll)t*t + t)/2;
}

ll f(int t,int x){
  return f_a(t) * x + f_b(t);
}

bool check(int f1, int f2, int f3){
  return (f_a(f2)-f_a(f1)) * (f_b(f3)-f_b(f2)) >= (f_b(f2)-f_b(f1)) * (f_a(f3)-f_a(f2));
}

int main(){
  int deq[SIZE];
  
  scanf("%d%lld%lld%lld",&n,&a,&b,&w);

  for(int i=1;i<=n;i++){
    scanf("%lld",d+i);
  }
  
  d[n+1] = 0;

  dp[0] = w;

  int s=0, t=1;
  deq[0] = 0;

  for(int i=1;i<=n+1;i++){
    while(s+1 < t && f(deq[s], i) >= f(deq[s+1],i)) s++;

    dp[i] = a * (1-i) + b * ((ll)i*i - i)/2 + d[i] + f(deq[s],i);
    
    while(s+1 < t && check(deq[t-2], deq[t-1], i)) t--;

    deq[t++] = i;
  }

  printf("%lld\n",dp[n+1]);
  
  return 0;
}
0