HDU 4701 - Game
题目大意是:有N个物品,每个物品有Ci个价值,ALICE和BOB分别有A, B元钱,依次购买(即买第i个物品前i-1物品必须买完),直到有一人无法购买。问ALICE是否有必胜策略。
博弈论和DP结合,具体见代码:
1 /* 2 ID:esxgx1 3 LANG:C++ 4 PROG:hdu4701 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 12 int _s[1000007], *s = &_s[1]; 13 int dp[1000007]; 14 15 int main(void) 16 { 17 #ifndef ONLINE_JUDGE 18 freopen("in.txt", "r", stdin); 19 #endif 20 21 int N, A, S; 22 while(scanf("%d%d%d", &N, &A, &S) > 0) { 23 S += A; 24 s[-1] = 0; 25 for(int i=0; i<N; ++i) { 26 int a; 27 scanf("%d", &a); 28 s[i] = s[i-1] + a; 29 } 30 /* O(n2) 31 for(int i=N; i--; ) { 32 dp[i] = s[N-1] - s[i-1]; 33 for(int j = i+1; j<N; ++j) 34 dp[i] = min(dp[i], max(s[j-1] - s[i-1], S - s[i-1] - dp[j] + 1)); 35 }*/ 36 int minn = s[N-1]; 37 for(int i=N; i--; ) { 38 dp[i] = min(s[N-1], minn) - s[i-1]; 39 minn = min(minn, max(s[i-1], S - dp[i] + 1)); 40 } 41 printf(A >= dp[0] ? "ALICE\n": "BOB\n"); 42 } 43 return 0; 44 }
2014-10-01 15:20:55 | Accepted | 4701 | 453MS | 8124K | 662 B | G++ |
![知识共享许可协议(Creative Commons License)](http://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png)
This article is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
本文采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。