「IOI1998」「LuoguP4342」Polygon(区间dp
题意翻译
题目可能有些许修改,但大意一致
多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4。每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记。

第一步,删除其中一条边。随后每一步:
选择一条边连接的两个顶点V1和V2,用边上的运算符计算V1和V2得到的结果来替换这两个顶点。
游戏结束时,只有一个顶点,没有多余的边。
如图所示,玩家先移除编号为3的边。之后,玩家选择计算编号为1的边,然后计算编号为4的边,最后,计算编号为2的边。结果是0。

(翻译者友情提示:这里每条边的运算符旁边的数字为边的编号,不拿来计算)
编写一个程序,给定一个多边形,计算最高可能的分数。
输入格式
输入描述一个有n个顶点的多边形,它包含两行。第一行是数字n,为总边数。
第二行描述这个多边形,一共有2n个读入,每两个读入中第一个是字符,第二个是数字。
第一个字符为第一条边的计算符号(t代表相加,x代表相乘),第二个代表顶点上的数字。首尾相连。
3 < = n < = 50
对于任何一系列的操作,顶点数字都在[-32768,32767]的范围内。
输出格式
第一行,输出最高的分数。在第二行,它必须写出所有可能的被清除后的边仍能得到最高得分的列表,必须严格递增。
感谢@2016c01 提供的翻译
题目描述
Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with either the symbol + (addition) or the symbol * (product). The edges are numbered from 1 to N.

On the first move, one of the edges is removed. Subsequent moves involve the following steps: �pick an edge E and the two vertices V1 and V2 that are linked by E; and �replace them by a new vertex, labelled with the result of performing the operation indicated in E on the labels of V1 and V2. The game ends when there are no more edges, and its score is the label of the single vertex remaining.
Consider the polygon of Figure 1. The player started by removing edge 3. After that, the player picked edge 1, then edge 4, and, finally, edge 2. The score is 0.

Write a program that, given a polygon, computes the highest possible score and lists all the edges that, if removed on the first move, can lead to a game with that score.
输入输出格式
输入格式:
Your program is to read from standard input. The input
describes a polygon with N vertices. It contains two lines. On the first
line is the number N. The second line contains the labels of edges 1,
..., N, interleaved with the vertices' labels (first that of the vertex
between edges 1 and 2, then that of the vertex between edges 2 and 3,
and so on, until that of the vertex between edges N and 1), all
separated by one space. An edge label is either the letter t
(representing +) or the letter x (representing *).
3 <= N <= 50
For any sequence of moves, vertex labels are in the range [-32768,32767].
输出格式:
Your program is to write to standard output. On the first line
your program must write the highest score one can get for the input
polygon. On the second line it must write the list of all edges that, if
removed on the first move, can lead to a game with that score. Edges
must be written in increasing order, separated by one space.
输入输出样例
题解
首先是对于环形问题的惯用套路——断环为链,再复制一遍。
对于链上的每个点,我们记录该点的初始值$num[i]$ 和该点左边的边的符号$fh[i]$
然后在状态设置上,不仅要设区间$[L,R]$合并得到的最大值$f[L][R]$,还要设一个区间最小值$g[L][R]$。
因为乘法的存在和负负得正的原理,两个为负的值会对结果产生正的贡献。
然后就是很常规的区间dp了。
/*
qwerta
P4342 [IOI1998]Polygon
Accepted
100
代码 C++,1.46KB
提交时间 2018-09-24 15:19:58
耗时/内存
18ms, 1044KB
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int num[];
bool fh[];
int f[][];
int g[][];
int stack[];//用来输出答案
int main()
{
//freopen("a.in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(false),cout.tie(false);//关闭同步流
int n;
cin>>n;
for(int i=;i<=n;++i)
{
char ch;
cin>>ch;
fh[i+n]=fh[i]=(ch=='t'?:);//记录符号 存的时候要存两截
int x;
cin>>x;
num[i+n]=num[i]=x;
}
int n2=n*;
memset(g,,sizeof(g));//给g赋INF
memset(f,,sizeof(f));//给f赋-INF
for(int i=;i<=n2;++i)
{
f[i][i]=g[i][i]=num[i];//赋初值
}
for(int len=;len<n2;++len)
for(int l=,r=len;r<=n2;++l,++r)
{
for(int k=l+;k<=r;++k)
if(!fh[k])//如果这里是一个加号
{
f[l][r]=max(f[l][r],f[l][k-]+f[k][r]);
g[l][r]=min(g[l][r],g[l][k-]+g[k][r]);
}
else//如果是个乘号
{
f[l][r]=max(f[l][r],max(f[l][k-]*f[k][r],g[l][k-]*g[k][r]));
g[l][r]=min(min(g[l][r],min(f[l][k-]*f[k][r],f[l][k-]*g[k][r])),
min(g[l][k-]*g[k][r],g[l][k-]*f[k][r]));
}
}
int ans=-,tos=;
for(int i=;i<=n;++i)
{
if(ans<f[i][i+n-])
{
ans=f[i][i+n-];
tos=;
stack[tos]=i;
}
else if(ans==f[i][i+n-])
{
stack[++tos]=i;
}
}//找答案
cout<<ans<<endl;
for(int i=;i<=tos;++i)
cout<<stack[i]<<" ";
return ;
}
「IOI1998」「LuoguP4342」Polygon(区间dp的更多相关文章
- 「kuangbin带你飞」专题二十二 区间DP
layout: post title: 「kuangbin带你飞」专题二十二 区间DP author: "luowentaoaa" catalog: true tags: - ku ...
- 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp
「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...
- 「CQOI2007」「BZOJ1260」涂色paint (区间dp
1260: [CQOI2007]涂色paint Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 2057 Solved: 1267[Submit][St ...
- IOI1998 Polygon [区间dp]
[IOI1998]Polygon 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘 ...
- 【IOI1998】Polygon 区间DP
题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条边 ...
- [IOI1998] Polygon (区间dp,和石子合并很相似)
题意: 给你一个多边形(可以看作n个顶点,n-1条边的图),每一条边上有一个符号(+号或者*号),这个多边形有n个顶点,每一个顶点有一个值 最初你可以把一条边删除掉,这个时候这就是一个n个顶点,n-2 ...
- POJ 1179 - Polygon - [区间DP]
题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a ...
- IOI 98 (POJ 1179)Polygon(区间DP)
很容易想到枚举第一步切掉的边,然后再计算能够产生的最大值. 联想到区间DP,令dp[i][l][r]为第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值是多少. 但是状态不好转移,因为操 ...
- 「LuoguP1430」 序列取数(区间dp
题目描述 给定一个长为n的整数序列(n<=1000),由A和B轮流取数(A先取).每个人可从序列的左端或右端取若干个数(至少一个),但不能两端都取.所有数都被取走后,两人分别统计所取数的和作为各 ...
- 「THUSC 2016」成绩单 & 方块消除 (区间dp)
成绩单 $f[l][r][mi][mx]$表示从l到r发到还没发的部分的最小值为mi最大值为mx时的最小代价. $f[l][r][0][0]$表示从l到r全部发完的代价. 自己写的无脑dp,枚举中转点 ...
随机推荐
- JavaScript闭包其一:闭包概论 函数式编程中一些基本定义
http://www.nowamagic.net/librarys/veda/detail/1707前面介绍了作用域链和变量对象,现在再讲闭包就容易理解了.闭包其实大家都已经谈烂了.尽管如此,这里还是 ...
- C#应该掌握的一些东西
C#应该掌握的一些东西 随着培训机构的增多,越来越多的人进入IT行业.那么对于我们这些自学出来,经验不够丰富的转行者来说,我们需要掌握最起码的一些东西,这对于面试很有用,而且在工作中也很常用.本人 ...
- .Net 平台WebService的创建、部署和使用介绍
.NET平台内建了对Web Service的支持,包括Web Service的构建和使用.与其它开发平台不同,使用.NET平台,你不需要其他的工具或者SDK就可以完成Web Service的开发了.. ...
- mysql + php 中文乱码 全是? 解决方法
在my.ini文件中找到[client]和[mysqld]字段,在下面均加上default-character-set=utf8,保存并关闭,重启服务器 在window下重启失败,这是因为你安装了高版 ...
- xadmin入门使用
,官方文档:http://xadmin.readthedocs.io/en/docs-chinese/views_api.html 中文文档:https://www.kancloud.cn/net_y ...
- 已迁移到http://www.coffin5257.com
点我直达
- Linux 下编译安装OpenCV(zhuanzai)
http://www.cnblogs.com/emouse/archive/2013/02/22/2922940.html Cmake的安装 OpenCV 2.2以后版本需要使用Cmake生成make ...
- Canvas学习笔记——动画环境中的边界
在动画中经常要处理边界问题,比如一个物体运动到了边界,要怎么处理才合适呢?通常有几种以下几种方式: 让物体消失 // > 16 & 0xff, g = color >> 8 ...
- unittest相关文档
文档链接: http://blog.csdn.net/wangst4321/article/details/8454118
- jquery根据(遍历)html()的内容/根据子元素的内容(元素文本)来选择(查询),在子元素前加入元素
<ul> <li>First</li> <li>second</li> <li>third</li> </ul ...