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(2 ≤ n ≤ 200 000) — 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( − 10 000 ≤ ai ≤ 10 000) — 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(2 ≤ n ≤ 200 000) — 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( − 10 000 ≤ ai ≤ 10 000) — 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[n−1]=sum[n],dp[1]
就是答案。
现在要计算dp[i]
,假设先手取第k
(
i
<
k
≤
n
i < k ≤ n
i<k≤n)个数,这会使先手的分数增加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<j≤n
如果能在计算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);
}