結果

問題 No.409 ダイエット
ユーザー 0w1
提出日時 2017-04-25 20:56:39
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,492 bytes
コンパイル時間 1,735 ms
コンパイル使用メモリ 174,880 KB
実行使用メモリ 7,016 KB
最終ジャッジ日時 2024-09-13 09:09:26
合計ジャッジ時間 5,109 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57 WA * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAXN = int( 3e5 );

int N, A, B, W;
int D[ MAXN ];
double dp[ MAXN + 1 ];

struct Line {
  double s, c; // slope, constant
  Line( double _s, double _c ) {
    s = _s;
    c = _c;
  }
  double operator() ( double x ) {
    return s * x + c;
  }
};

bool check( Line l1, Line l2, Line l3 ) {
  double x12 = ( l2.c - l1.c ) / ( l1.s - l2.s );
  return l3( x12 ) <= l1( x12 );
}

signed main() {
  ios::sync_with_stdio( 0 );
  cin >> N >> A >> B >> W;
  for( int i = 0; i < N; ++i ) {
    cin >> D[ i ];
  }
  if( B == 0 ) {
    cout << 1LL * W - 1LL * A * N << endl;
    exit( 0 );
  }
  deque< Line > dq;
  dq.emplace_back( Line( 0.0, ( dp[ 0 ] = 1.0 * W ) + A ) );
  for( int i = 1; i <= N; ++i ) {
    while( dq.size() >= 2 and dq[ 0 ]( i ) >= dq[ 1 ]( i ) ) {
      dq.pop_front();
    }
    dp[ i ] = dq[ 0 ]( i ) + ( 1.0 * i * i - i ) * B / 2 - 1.0 * i * A + D[ i - 1 ];
    Line newl( -1.0 * B * i, dp[ i ] + ( 1.0 * i * i + i ) * B / 2 + 1.0 * ( i + 1 ) * A );
    while( dq.size() >= 2 and check( dq[ 0 ], dq[ 1 ], newl ) ) {
      dq.pop_back();
    }
    dq.emplace_back( newl );
  }
  double ans = 1e12;
  for( int i = 0; i <= N; ++i ) {
    ans = min( ans, dp[ i ] + 1.0 * B * ( N - i ) * ( N - i + 1 ) / 2 - 1.0 * A * ( N - i ) );
  }
  long long v = int( ans );
  if( fabs( v - ans ) > fabs( v + 1 - ans ) ) {
    ++v;
  } else if( fabs( v - ans ) > fabs( v - 1 - ans ) ) {
    --v;
  }
  cout << v << endl;
  return 0;
}
0