文档章节

sicily 1176 Two Ends

Ciel
 Ciel
发布于 2013/01/30 20:48
字数 656
阅读 242
收藏 0

Description

In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest -- we'll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.) 
3 2 10 4 
You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.

Input

There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.

Output

For each test case you should print one line of output of the form: 
In game m, the greedy strategy might lose by as many as p points. 
where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player's score and second player's score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.

Sample Input

4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0

Sample Output

In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

分析:

如果只是为了正确,深搜是种可行的办法,但是显然数据规模不小,深搜要超时。用空间换时间,用DP的办法做,也算是中记忆化搜索。每次抽牌方法需要比较的是抽出两端任意一张后的取法最大所得,而另一人取法是贪心,递归可求。

代码:

// Problem#: 1176
// Submission#: 1914687
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstring>
using namespace std;

#define MAX 1000

int num[MAX];
int buf[MAX][MAX];

inline int max(int a, int b) { return a > b ? a : b; }

int dp(int a, int b) {
    if (a == b-1) return buf[a][b] = max(num[a], num[b]);
    if (buf[a][b]) return buf[a][b];
    int t1, t2;
    t1 = num[a];
    t1 += (num[a + 1] >= num[b]) ? dp(a + 2, b) : dp(a + 1, b - 1);
    t2 = num[b];
    t2 += (num[a] >= num[b - 1]) ? dp(a + 1, b - 1) : dp(a, b - 2);
    return buf[a][b] = max(t1, t2);
}

int main(void) {
    int n, s, cnt = 1;
    while (cin >> n && n) {
        s = 0;
        for (int i = 0; i < n; ++i) {
            cin >> num[i];
            s += num[i];
        }
        memset(buf, 0, sizeof(buf));
        cout << "In game " << cnt++ << ", the greedy strategy might lose by as many as " << 2 * dp(0, n - 1) - s << " points." << endl;
    }
    return 0;
}

© 著作权归作者所有

上一篇: sicily 2502 买珍珠
下一篇: sicily 1563 GECKO
Ciel
粉丝 3
博文 24
码字总数 18532
作品 0
程序员
私信 提问
Oracle 11g R2 ADG 搭建

--============Oracle ADG搭建============== --==========准备阶段========= 1.检查primary为archivelog模式。 select log_mode from v$database; 如果为noarchivelog模式,切换到archivelo......

UltraSQL
2018/07/23
0
0
PHP微信柏拉图性格标签生成器源码

演示参考:http://www.erdangjiade.com/php/1176.html 效果图片: 前台页面: 数据处理: 演示参考:http://www.erdangjiade.com/php/1176.html...

2当家的
2017/02/24
420
0
ARM11处理器的系统建模解决方案

【IT168 技术文章】 ARM公司发布了RealView? ESL工具,用于基于ARM11?系列处理器系统的建模。通过在基于周期(cycle-based)和交互(transaction-based)的抽象层使用RealView ESL工具可以简化设...

IT168网站
2009/10/29
0
0
如何用python批量做图片的直方图投影和投影数据,并将数据存在另一个excel中

同一个文件夹下:有n多张棒状图图片和一个list.txt ,包含文件夹下所有的图片名称; 想设计一个python程序,如何按照list.txt中的顺序批量将图的信息转成数字信息,并按list.txt中顺序保存在...

smallfatQQ
2018/08/28
388
1
如何在SCO 3.2V4.2和OSR5下安装配置SNMP

翻译, 原文请看: http://members.cruzio.com/~jeffl/sco/snmp_install.txt 1、登录root帐户 # mkdev snmp 2、编辑/etc/snmpd.conf 注意snmpd.conf中的descr和objid的内容都是固定的,不要修改...

范堡
2009/05/05
147
0

没有更多内容

加载失败,请刷新页面

加载更多

使用递归打印乘法表

一般我们在学for循环的时候都会去打印九九乘法表,但是如果是用递归的方式打印的话,应该怎么做呢? 下面讲解一下用递归打印九九乘法表的思路: 其实我们在用for循环打印乘法表的时候,用的是...

INEVITABLE
9分钟前
1
0
sql 练习

创建需要的4张表 首先创建student、course、score、teacher这四张表。 student表 创建student表 CREATE TABLE IF NOT EXISTS student(sno TINYINT UNSIGNED NOT NULL,sname VARCHAR(20......

Garphy
10分钟前
0
0
vSphere的两种虚拟交换机

VMware vSphere 6.7中支持两种虚拟交换机: 1、标准交换机,VSS - Virtual Standard Switch 2、分布式交换机,VDS - Virtual Distributed Switch VSS与ESXi主机一一对应,即一个VSS只能部署在...

大别阿郎
12分钟前
1
0
webGL和three.js的关系

本文转载于:专业的前端网站➤webGL和three.js的关系 如今浏览器的功能越来越强大,而且这些功能可能通过JavaScript直接调用。你可以用HTML5标签轻松地添加音频和视频,而且可以在HTML5画布上...

前端老手
40分钟前
6
0
Spring如何实现AOP,请不要再说cglib了!

1. 从注解入手找到对应核心类 最近工作中我都是基于注解实现AOP功能,常用的开启AOP的注解是@EnableAspectJAutoProxy,我们就从它入手。 上面的动图的流程的步骤就是: @EnableAspectJAutoPr...

温安适
42分钟前
38
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部