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;
}
Codeforces 731D Funny Game的更多相关文章
- 【codeforces 731D】80-th Level Archeology
[题目链接]:http://codeforces.com/contest/731/problem/D [题意] 给你n个象形文; 每个象形文由l[i]个数字组成; 你可以把所有的组成象形文的数字同时增 ...
- CodeForces 731D 80-th Level Archeology
区间并. 对于上下两个数字,如果不一样,那么可以计算出哪一段范围内可以保证字典序,并且后面所有位置都无需再考虑.对所有范围求交集就是答案了. 求交集写起来有点烦,直接对不可取的范围求并即可. #pra ...
- CodeForces 731D (差分+线段扫描)
Description Archeologists have found a secret pass in the dungeon of one of the pyramids of Cyclelan ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- VC++判断是否连网
在开发中,需要判断是否有网络连接,于是写了个函数,实现代码如下: //判断是否有网络连接 static BOOL DoHaveInternetConnection() { BOOL bRet = FA ...
- Linux中Curl命令couldn't connect to host解决方案 php操作Curl(http,https)无法获取远程数据解决方案
本人在做百度账户第三方登录接口,获取百度token,利用php操作curl post方式发送请求token,出现couldn't connect to host错误.经过调试测试,最后终于成功.回头写 ...
- C# 反射之属性操作
一.反射-类操作 //1.获取对象所有的属性名 Student stu = new Student(); //获取当前类名称 Console.WriteLine(stu.GetType().Name) ...
- 如何在Sql2008中获取表字段属性和注释?
如何在Sql2008中获取表字段属性和注释? select b.[value] from sys.columns a left join sys.extended_properties b on a. ...
- play app to war
project/Build.scala import sbt._ import Keys._ import play.Play.autoImport._ import PlayKeys._ impor ...
- 深入了解shell
接触linux很久了,但一直没有总线,老是尝鲜,什么都想学,但好多没多没有记住,特的总结了一些基本的东西,查了很多资料,不完善的方面我会慢慢的更新…… 操作系统与外部最主要的接口就叫做shell. ...
- ios专题 - GCD(2)
何为Dispatch Sources 简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一个block放入一个dispatch queue的执行例程中. ...
- [转]jQuery源码分析系列
文章转自:jQuery源码分析系列-Aaron 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://github.com/JsAaro ...
- 百度前端技术学院(IFE)2016春季学期总结
今天(5月16日)作为第八个提交者提交了任务五十:RIA微型问卷管理平台 这样一个综合性的大任务,宣告我的IFE春季学期课程学习顺利完成.其实任务五十并不复杂,现在再让我来做,可能一周不到就写出来了, ...
- return、break、continue的区别
一.定义 return:语句用于终止当前方法运行,返回到调用该方法的语句处.如有返回值,则在return后面提供相应的返回值. break:其主要用于循环语句和switch语句,其作用均是强行终止语句 ...