UVa 10891 - Game of Sum 動態規劃,博弈 難度: 0
阿新 • • 發佈:2019-02-24
動態規劃 closed org line 時間 alt algo hid queue
題目
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1832
題意
n個數據,A,B兩個玩家輪流從兩端取1到多個數字,最終數字加和為分數,分數最大的獲勝,A為先手,二者都很聰明,求A分數-B分數。
思路
如劉書,
區間DP,明顯可以用a[i][j]記錄區間[i,j)的先手最大分數。
感想
1. 要像劉一樣,在能達到要求之後進一步思考如何減少時間
代碼
#include <algorithm> #includeView Code<cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <tuple> #define LOCAL_DEBUG using namespace std; typedef pair<int, int> MyPair;const int MAXN = 101; int sum[MAXN][MAXN]; int a[MAXN][MAXN]; int mnleftA[MAXN][MAXN]; int mnrightA[MAXN][MAXN]; int g[MAXN]; int main() { #ifdef LOCAL_DEBUG freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin); //freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);#endif // LOCAL_DEBUG int n; for (int ti = 1; scanf("%d", &n) == 1 && n; ti++) { for (int i = 0; i < n; i++)scanf("%d", g + i); for (int i = 0; i < n; i++) { sum[i][i] = 0; for (int j = i + 1; j <= n; j++) { sum[i][j] = sum[i][j - 1] + g[j - 1]; } } for (int len = 1; len <= n; len++) { for (int i = 0, j = i + len; j <= n; i++, j++) { a[i][j] = sum[i][j] - min(mnleftA[i][j - 1], mnrightA[i + 1][j]); mnleftA[i][j] = min(mnleftA[i][j - 1], a[i][j]); mnrightA[i][j] = min(mnrightA[i + 1][j], a[i][j]); } } int ans = 2 * a[0][n] - sum[0][n]; printf("%d\n", ans); } return 0; }
UVa 10891 - Game of Sum 動態規劃,博弈 難度: 0