hdu 6169 gems gems gems【DP】

题目链接:hdu 6169 gems gems gems

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.

 

题目意思转化为第i个人希望最大化与第i+1个人的差值。

dp[i][j]表示当前人从第i个宝石开始取j个的最大差值。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int N = 20001;
 6 const int M = 201;
 7 int a[N];
 8 int sum[N];
 9 int dp[N][M];
10 int main() {
11     int t, n, i, j, ans;
12     scanf("%d", &t);
13     sum[0] = 0;
14     while(t--) {
15         scanf("%d", &n);
16         for(i = 1; i <= n; ++i) {
17             scanf("%d", &a[i]);
18             sum[i] = sum[i-1] + a[i];
19         }
20         for(i = n; i >= 1; --i) {
21             for(j = min(200, n-i+1); j >= 1; --j) {
22                 dp[i][j] = sum[i+j-1] - sum[i-1];
23                 if(i+j+j <= n)dp[i][j] -= max(dp[i+j][j], dp[i+j][j+1]);
24                 else if(i+j+j-1 <= n) dp[i][j] -= dp[i+j][j];
25             }
26         }
27         ans = dp[1][1];
28         if(n>1) ans = max(dp[1][1], dp[1][2]);
29         printf("%d\n", ans);
30     }
31     return 0;
32 }
249ms

 

posted @ 2017-09-18 16:58  GraceSkyer  阅读(237)  评论(0编辑  收藏  举报

~~~~~~ACM大牛语录,激励一下~~~~~~

为了世界的和平,为了女生的安全,我拼命做题,做题,做题!

用最短的时间,刷最多的题!

给我一滴泪,我就看到了你全部的海洋!

seize the hour, seize the day.

人生难免有无奈,幸福走远了,或是感叹幸福来迟了.其实我一直相信,无论手中的幸福是多么微不足道的感觉,我会把握住那每一分,每一秒,当幸福依旧像那百鸟般飞逝,终究无法掌握时,我会感谢它,曾经降临过!

A自己的题,让别人郁闷去吧

WA肠中过,AC心中留 TLE耳边过,AC特别牛

天然的悲苦和伤逝,过去有过,以后还会有

^*^一步一步往上爬^*^

AC就像练级,比赛就像PK. 练级不如PK好玩

其实,世上本没有ACM,AC的人多了,也便有了!

AC无止尽~ Seek you forever~

找呀找呀找水题,找到一个AC一个呀!

AC是检验程序的唯一标准。

真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血……