結果
問題 |
No.973 余興
|
ユーザー |
![]() |
提出日時 | 2020-01-17 23:16:46 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,815 bytes |
コンパイル時間 | 1,590 ms |
コンパイル使用メモリ | 169,976 KB |
実行使用メモリ | 206,528 KB |
最終ジャッジ日時 | 2024-06-26 00:29:56 |
合計ジャッジ時間 | 13,134 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 2 |
other | AC * 11 TLE * 1 -- * 42 |
ソースコード
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll N,X; vector<ll> a; int dp[5010][5010][2]; //閉じた区間[l,r]について考える bool rec(ll l, ll r, bool turn){ int c = 0; if(turn){ c = 1;} if( dp[l][r][c] != -1){ return dp[l][r][c];} if( r-l == 0){ //1つのみが残っている時 このターンに回ってきた方が負ける return !turn; } else{ ll left = l; ll right = r; ll res1 = 0,res2 = 0; if( turn ){ // Aのターンの時 while(left < r && res1+a[left] <= X){ res1 += a[left]; left++; if( rec(left,r,!turn) == true ){ dp[l][r][1] = 1; return true;} //一つでもtrueが返せる瞬間があったら、Aは勝てる } while(right > l && res2+a[right] <= X){ res2 += a[right]; right--; if( rec(l,right,!turn) == true ){ dp[l][r][1] = 1; return true;} } dp[l][r][1] = 0; return false; } else{ while(left < r && res1+a[left] <= X){ res1 += a[left]; left++; if( rec(left,r,!turn) == false ){ dp[l][r][0] = 0;return false;} } while(right > l && res2+a[right] <= X){ res2 += a[right]; right--; if( rec(l,right,!turn) == false){ dp[l][r][0] = 0; return false;} } dp[l][r][0] = 1; return true; } } } int main(){ cin >> N >> X; a.resize(N); for(ll i = 0; i < N; i++){ cin >> a[i];} for(int i = 0; i < 5010; i++){ for(int j = 0; j < 5010; j++){ dp[i][j][0] = -1; dp[i][j][1] = -1; } } bool res = rec(0,N-1,true); if( res ){ cout << "A" << endl;} else{ cout <<"B" << endl;} return 0; }