Codeforces 731D Funny Game

Description

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 \le 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 \le n \le 200 000 )\) — the number of stickers,initially located on the wall.
The second line contains \(n\) integers \(a_1 , a_2 , \dots , a_n\) \(( ­ 10 000 \le a_i \le10 000 )\) — the numbers on stickers in order from left to right.

Output

Print one integer — the difference between the Petya's score and Gena's score at the end of the game if both players play optimally.

Sample Input

3
2 4 8

Sample Output

4
1 -7 -2 3

比赛时想了个dp,过了pretest,但是fst了。算法的确有很大的bug(居然还能过pretest,还没人hack。。。)

考虑最优解,它一定时\(a_{i_{1}}-a_{i_{2}}+\cdots+(-1)^{t-1}a_{i_{t}}+(i-1)^ta_n\)

但是我们无法确定直接确定\(a_{i_1}\)的值,但是最后一个肯定是\(a_n\),所以我们可以从后边往前边推。由于两个人其实是等价的,对于已经确定好的一段后缀,很明显我们可以通过贪心的方法来使答案尽可能地优。设当前的\([i+1,n]\)这段后缀的答案为\(ans\),我们枚举第\(i\)个,则有$$ans = max(ans,a_i+(-ans))$$

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

#define maxn (200010)
#define inf (1<<30)
int pre[maxn],N,mx;

int main()
{
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
	scanf("%d",&N);
	for (int i = 1;i <= N;++i) scanf("%d",pre+i),pre[i] += pre[i-1];
	mx = pre[N];
	for (int i = N-1;i > 1;--i) mx = max(mx,pre[i]-mx);
	printf("%d",mx);
	fclose(stdin); fclose(stdout);
	return 0;
}
posted @ 2016-11-01 23:08  lmxyy  阅读(226)  评论(0编辑  收藏  举报