「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,枚举中转点 ...
随机推荐
- Shadow Map阴影贴图技术之探 【转】
这两天勉勉强强把一个shadowmap的demo做出来了.参考资料多,苦头可不少.Shadow Map技术是目前与Shadow Volume技术并行的传统阴影渲染技术,而且在游戏领域可谓占很大优势.本 ...
- POJ 3580(SuperMemo-Splay区间加)[template:Splay V2]
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11384 Accepted: 3572 Case T ...
- 【转载】FAT32文件系统详解
硬盘是用来存储数据的,为了使用和管理方便,这些数据以文件的形式存储在硬盘上.任何操作系统都有自己的文件管理系统,不同的文件系统又有各自不同的逻辑组织方式.例如:常见的文件系统有FAT,NTFS,EXT ...
- On ROWNUM and Limiting Results
This issue's Ask Tom column is a little different from the typical column. I receive many questions ...
- 04 svn设置开机启动
一:设置开机启动[dos命令] 格式: sc create 服务名称 binPath=空格"安装目录/svnserve.exe -r 版本库地址目录 --service" star ...
- scikit-learn:4.2. Feature extraction(特征提取,不是特征选择)
http://scikit-learn.org/stable/modules/feature_extraction.html 带病在网吧里. ..... 写.求支持. .. 1.首先澄清两个概念:特征 ...
- Operation not permitted - /usr/bin/pod
问题描述:执行sudo gem install cocoapods, 提示出错:While executing gem ... (Errno::EPERM) Operation not per ...
- 2251: [2010Beijing Wc]外星联络
2251: [2010Beijing Wc]外星联络 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 801 Solved: 481[Submit][ ...
- 九度OJ 1096:日期差值 (日期计算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8138 解决:2752 题目描述: 有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天 输入: 有多组数据, ...
- HDOJ 4689 Derangement DP
DP具体解释见: http://blog.csdn.net/liguan1/article/details/10468139 Derangement Time Limit: 7000/7000 MS ...