【Codeforces Round #376 (Div. 2)】 Codeforces 731E Funny Game

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)
— the number of stickers, initially located on the wall.

The second line contains n integers a1, a2, …, an
( - 10 000 ≤ ai ≤ 10 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.

用dp[i]表示已经把1..i合成一堆,接下来先手的最好结果。
接下来会把i..j合成一堆,也就是dp[i]=max{sum[j]-dp[j]},i+1< =j< =n
从后往前扫描的时候顺便维护一下sum[]-dp[]的最大值,时间复杂度O(n)。

#include<cstdio>
#include<cstring>
#define LL long long
LL dp[200010],sum[200010],a[200010];
LL max(LL x,LL y)
{
    return x>y?x:y;
}
int main()
{
    int i,j,k,m,n,p,q,x,y,z;
    LL mx;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
      scanf("%I64d",&a[i]),sum[i]=sum[i-1]+a[i];
    mx=sum[n];
    for (i=n-1;i;i--)
    {
        dp[i]=mx;
        mx=max(mx,sum[i]-dp[i]);
    }
    printf("%I64d\n",dp[1]);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值