Codeforces 731E Funny Game 【逆推DP、博弈】

Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won’t stop them.

The game they came up with has the following rules. Initially, there are n stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first.

One move happens as follows. Lets say there are m ≥ 2 stickers on the wall. The player, who makes the current move, picks some integer k from 2 to m and takes k leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move.

Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent.

Given the integer n and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya’s and Gena’s score if both players play optimally.

Input

The first line of input contains a single integer n ( 2   ≤   n   ≤   200   000 ) n (2 ≤ n ≤ 200 000) n(2n200000) — the number of stickers, initially located on the wall.

The second line contains n n n integers a 1 , a 2 , . . . , a n (   −   10   000   ≤   a i   ≤   10   000 ) a_1, a_2, ..., a_n ( - 10 000 ≤ a_i ≤ 10 000) a1,a2,...,an(10000ai10000) — the numbers on stickers in order from left to right.

Output

The first line of input contains a single integer n ( 2   ≤   n   ≤   200   000 ) n (2 ≤ n ≤ 200 000) n(2n200000) — the number of stickers, initially located on the wall.

The second line contains n n n integers a 1 , a 2 , . . . , a n (   −   10   000   ≤   a i   ≤   10   000 ) a_1, a_2, ..., a_n ( - 10 000 ≤ a_i ≤ 10 000) a1,a2,...,an(10000ai10000) — the numbers on stickers in order from left to right.

题意

有一列数和两个人,两个人轮流从这列数的左侧取2-m个数,并将取到的数的和添加在这列数的左侧,直到只剩下一个数。分数等于这个人取到的所有数的和,两个人都想使自己的分数-对方的分数最大,求先手分数-后手分数

思路

这道题相当于两个人轮流从前缀和数列中取数,每次取数只能从上次取的数的后边开始取,直到取完最后一个数,而且第一个数是不能取的。分数就是这个人取的所有数的和。两个人的目的都是使自己分数-对方分数尽可能的大,输出先手分数-后手分数

假设两个人已经取了一部分数,那么接下来的取法和之前取过的数是没有关系的,只与之后的取法有关,这说明应该逆推。

dp[i]表示在先手在 [ i + 1 , n ] [i + 1,n] [i+1,n]中开始取数(这里的数指的是前缀和)时,先手分数-后手分数能达到的最大值,则 d p [ n − 1 ] = s u m [ n ] dp[n - 1]=sum[n] dp[n1]=sum[n]dp[1]就是答案。

现在要计算dp[i],假设先手取第k i < k ≤ n i < k ≤ n i<kn)个数,这会使先手的分数增加sum[k],在先手取完后,轮到后手,后手只能在 [ k + 1 , n ] [k + 1,n] [k+1,n]这个范围取数,后手也想使自己的分数与对手的分数的差值尽可能的大,但是似乎没有记录后手开始从 [ k + 1 , n ] [k + 1,n] [k+1,n]中取数时能产生的最大差值(后手分数-先手分数,但是实际上这个值就是dp[k],因为这就相当于后手从第k + 1个位置开始先手,也就是说,如果先手取了第k个数,那么后手接下来能够得到的最大差值(后手分数-先手分数)就是dp[k] - sum[k](因为先手取了第k个数),那么先手取第k个数的最大差值(先手分数-后手分数)就是sum[k] - dp[k]。由此就可以得到状态转移方程。

d p [ i ] = m a x ( s u m [ j ] − d p [ j ] ) , i < j ≤ n dp[i] = max(sum[j] - dp[j]), i<j≤n dp[i]=max(sum[j]dp[j]),i<jn

如果能在计算dp[i]的同时记录下sum[i]-dp[i]~sum[n]-dp[n]的最大值,那么只需要 O ( n ) O(n) O(n)的时间就可以完成递推。

代码

#include <cstdio>
#include <algorithm>

using namespace std;

int n, ans, a[200010];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]), a[i] += a[i - 1];
	ans = a[n];
	for (int i = n - 1; i > 1; i--)
		ans = max(ans, a[i] - ans);
	printf("%d\n", ans);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值