1. 程式人生 > >HDU 6199 gems gems gems (2017瀋陽網賽

HDU 6199 gems gems gems (2017瀋陽網賽

題意:

有一堆數, 兩個人輪流取, 只能從最左邊開始選擇, 假設上一個人選了k 個牌, 那麼下一個人只能選擇k 或 k + 1 張牌。 第一個人得分為A, 第二個人得分為B, 求A- B, 每個人的策略都想使自己得分儘可能的高。

思路:

是UVA 10891 的變形把。

我們令dp[i][j] 表示從i 位置開始選擇, 能選j 個牌的最大分數差值。

那麼直接根據j 來轉移即可。

總共兩種選擇, 要麼選j 個, 要麼選j + 1個, 處理個字首和即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;


const int maxn = 20000 + 1;
const int inf = 0x3f3f3f3f;
int dp[maxn][200];
bool vis[maxn][200];
int a[maxn], n;
int sum[maxn], ks;
int dfs(int pos, int k){
    int& ans = dp[pos][k];
    if (vis[pos][k] == 1) return ans;
    vis[pos][k] = 1;

    if (pos > n){
        return ans = 0;
    }

    if (pos + k - 1 > n){
        return ans = 0;
    }

    ans = -inf;

    int lak = pos + k - 1;

    int lak1 = pos + k;

    if (lak <= n){
        ans = max(ans, sum[lak] - sum[pos - 1] - dfs(lak + 1, k));
    }
    if (lak1 <= n){
        ans = max(ans, sum[lak+1] - sum[pos - 1] - dfs(lak1 + 1, k + 1));
    }

    return ans;
}
int main(){

    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d", &n);
        memset(vis,0,sizeof vis);
        for (int i = 1; i <= n; ++i){
            scanf("%d", &a[i]);
            sum[i] = sum[i-1] + a[i];
        }
        printf("%d\n", dfs(1, 1));
    }
    return 0;
}

gems gems gems

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1296    Accepted Submission(s): 276


Problem Description Now there are n gems, each of which has its own value. Alice and Bob play a game with these n gems.
They place the gems in a row and decide to take turns to take gems from left to right. 
Alice goes first and takes 1 or 2 gems from the left. After that, on each turn a player can take k
 or k+1 gems if the other player takes k gems in the previous turn. The game ends when there are no gems left or the current player can't take k or k+1 gems.
Your task is to determine the difference between the total value of gems Alice took and Bob took. Assume both players play optimally. Alice wants to maximize the difference while Bob wants to minimize it.

Input The first line contains an integer T
 (1T10), the number of the test cases. 
For each test case:
the first line contains a numbers n (1n20000);
the second line contains n numbers: V1,V2Vn. (100000Vi100000)

Output For each test case, print a single number in a line: the difference between the total value of gems Alice took and the total value of gems Bob took.

Sample Input 1 3 1 3 2
Sample Output 4
Source
Recommend liuyiding   |   We have carefully selected several similar problems for you:  6205 6204 6203 6202 6201