結果
問題 |
No.409 ダイエット
|
ユーザー |
|
提出日時 | 2017-04-25 20:25:03 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,583 bytes |
コンパイル時間 | 1,804 ms |
コンパイル使用メモリ | 174,008 KB |
実行使用メモリ | 7,016 KB |
最終ジャッジ日時 | 2024-09-13 09:09:00 |
合計ジャッジ時間 | 13,026 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 17 WA * 20 RE * 55 |
ソースコード
#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 ); assert( fabs( l1( x12 ) - l2( x12 ) ) < 1e-9 ); double x13 = ( l3.c - l1.c ) / ( l1.s - l3.s ); return x13 <= 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 ], dq[ 2 ] ) ) { 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; }