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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- Linux kernel驱动相关抽象概念及其实现 之“bus,device,driver”
bus,device,driver三个很重要的概念贯穿Linux内核驱动架构,特转载一篇博文: (转载自http://blog.csdn.net/gdt_a20/article/details/642 ...
- HDU 1124 Factorial (数论)
http://acm.hdu.edu.cn/showproblem.php? pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長.我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙 ...
- Myapplication的作用与理解
1.前沿在设置里面,对应于某一个应用,虽然没有启动,但是上面有一个终止按钮,说明这个应用时启动的,只是里面没有Activity,所以说使用android退出把一个应用退出,实际上他所对应的Applic ...
- 字符串匹配算法-BM
在用于查找子字符串的算法中,BM(Boyer-Moore)算法是当前有效且应用比较广泛的一种算法,各种文本编辑器的“查找”功能(Ctrl+F),大多采用Boyer-Moore算法.比我们学习的KMP算 ...
- 使用CSS的类名交集复合选择器
首先先看一下基本定义: 复合选择器就是两个或多个基本选择器,通过不同方式连接而成的选择器,主要包括“交集”选择器.“并集”选择器.“后代”选择器. 交集选择器 “交集”复合选择器是由两个选择器直接连接 ...
- linux下实现tomcat定时自动重启
tomcat自带的脚本中没有提供直接restart的模式,但是有start和shutdown两种模式.要实现restart模式,实际上只需要判断是否已经启动tomcat,若已经启动则限制性shutdo ...
- PHP 支持的协议/封装协议列表
附录 L. 支持的协议/封装协议列表 目录 文件系统 Socket HTTP 和 HTTPS FTP 和 FTPS PHP 输入/输出流 压缩流 Secure Shell 2 音频流 以下是 PHP ...
- JKXY的视频内容下载工具类
package cn.jsonlu.make.license; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONOb ...
- memcached 安装
安装 memcached 需要 三部1,下载 memcached 放到php目录将php_memcached.dll 放到php的ext 目录 2,打开管理员命令,将memcached 拖拉到命令中, ...
- xml--小结④DTD的验证问题,不要用IE检验,IE不靠谱
可以自己写代码 <script type="text/javascript"> var xmldoc = new ActiveXObject("Microso ...