P4342 [IOI1998]Polygon
题意翻译
题目可能有些许修改,但大意一致
多边形是一个玩家在一个有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.
输入输出样例
4
t -7 t 4 x 2 x 5
33
1 2
Solution:
吐槽:循环时区间范围忘了乘$2$,卡了我好久好久啊`!~~~
很容易由题意想到要破环成链,然后想到用区间$DP$,定义状态$f[i][j]$表示区间$[i,j]$之间能计算出的最大值。
但是由于有乘法存在,这样很显然有后效性(因为区间$f[i,j]$的最大值有可能是由两个为负的最小值$f[i,k]*f[k+1,j]$得来),而加法显然不存在这种情况(只可能是最大值转移而来)。
于是,我们多定义一个状态存一段区间能计算出的最小值就好了。又由于本题是个环,所以我们直接倍长原序列。
设$f1[i][j]$表示区间$[i,j]$能计算出的最大值,$f2[i][j]$表示$[i,j]$能计算出的最小值,初始状态$f1[i][i]=f1[i+n][i+n]=f2[i][i]=f2[i+n][i+n]=x,i\in[1,n]$表示每个数不计算的值就是其本身,其余的$f1$赋值为$-inf$、$f2$赋值为$inf$。
考虑符号为$*$时(加法就正常转移$f1[i][j]$取$max$,$f2[i][j]$取$min$,就不多赘述了):
最大值有两种情况:由两段区间的最大值乘积转移过来,或者两段区间的最小值乘积转移。
最小值有两种情况:由第一段区间最大值和第二段区间最小值转移,或者由第一段区间最小值和第二段区间最大值转移过来。
所以不难得到符号为$*$的状态转移方程:
$f1[i][j]=max(f1[i][j],max(f1[i][k]*f1[k+1][j],f2[i][k]*f2[k+1][j]))$
$f2[i][j]=min(f2[i][j],min(f2[i][k]*f1[k+1][j],f1[i][k]*f2[k+1][j]))$
目标状态为$f1[i][i+n-1],i\in[1,n]$中的$max$值。
代码:
#include<bits/stdc++.h>
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)>(b)?(b):(a))
using namespace std;
const int N=;
int n,x,f1[N][N],f2[N][N],ans=-;
bool pd[N];
char s;
int main(){
ios::sync_with_stdio();
cin>>n;
memset(f1,-0x3f,sizeof f1);
memset(f2,0x3f,sizeof f2);
For(i,,n){
cin>>s>>x;
if(s=='x')pd[i]=pd[i+n]=;
f1[i][i]=f1[i+n][i+n]=f2[i][i]=f2[i+n][i+n]=x;
}
For(i,,n-) For(l,,n<<){
if(l+i>*n)break;
int r=l+i;
For(k,l,r-)
if(!pd[k+]){
f1[l][r]=Max(f1[l][r],f1[l][k]+f1[k+][r]);
f2[l][r]=Min(f2[l][r],f2[l][k]+f2[k+][r]);
}
else {
f1[l][r]=Max(f1[l][r],Max(f1[l][k]*f1[k+][r],f2[l][k]*f2[k+][r]));
f2[l][r]=Min(f2[l][r],Min(f1[l][k]*f2[k+][r],f2[l][k]*f1[k+][r]));
}
}
For(i,,n)ans=Max(f1[i][i+n-],ans);
cout<<ans<<endl;
For(i,,n)if(f1[i][i+n-]==ans)cout<<i<<' ';
return ;
}
P4342 [IOI1998]Polygon的更多相关文章
- luogu P4342 [IOI1998]Polygon
IOI早期这么多dp? 题目要求断掉环上的一边,我们可以断环为链,开两倍数组 容易想到dp,设\(f_{i,j}\)为区间\([i,j]\)的最大值,然后就是个枚举断点的区间dp 不过可能会有负数出现 ...
- 洛谷 P4342 [IOI1998]Polygon
题目传送门 解题思路: 一道环形dp,只不过有个地方要注意,因为有乘法,两个负数相乘是正数,所以最小的数是负数,乘起来可能比最大值大,所以要记录最小值(这道题是紫题的原因). AC代码: #inclu ...
- IOI1998 Polygon [区间dp]
[IOI1998]Polygon 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘 ...
- [IOI1998]Polygon(区间dp)
[IOI1998]Polygon 题意翻译 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条 ...
- 【洛谷P4342】[IOI1998]Polygon
Polygon 比较裸的环形DP(也可以说是区间DP) 将环拆成链,复制到后面,做区间DP即可 #include<iostream> #include<cstdio> usin ...
- 【洛谷 P4342】[IOI1998]Polygon(DP)
题目链接 题意不再赘述. 这题和合并石子很类似,但是多了个乘法,而乘法是不满足"大大得大"的,因为两个非常小的负数乘起来也会很大,一个负数乘一个很大的整数会很小,所以我们需要添加一 ...
- POJ 1179 IOI1998 Polygon
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5472 Accepted: 2334 Description Polyg ...
- [IOI1998]Polygon
很早就看到这题了...但因为有个IOI标志,拖到现在才做 由于是以前在书上看到的,就没有想过其他算法,直接区间DP了... 方程式也挺好想的 跟我们平时做数学题求几个数乘积最大差不多 最大的*最大的 ...
- [IOI1998] Polygon (区间dp,和石子合并很相似)
题意: 给你一个多边形(可以看作n个顶点,n-1条边的图),每一条边上有一个符号(+号或者*号),这个多边形有n个顶点,每一个顶点有一个值 最初你可以把一条边删除掉,这个时候这就是一个n个顶点,n-2 ...
随机推荐
- SpringBoot学习7:springboot整合jsp
springboot内部对jsp的支持并不是特别理想,而springboot推荐的视图是Thymeleaf,对于java开发人员来说还是大多数人员喜欢使用jsp 1.创建maven项目,添加pom依赖 ...
- MySQL视图、事务
view(视图):虚拟表主要用来看(查)数据基表的数据变化会在视图中体现出来 权限控制将多表查询的结果整合在视图中方便用户查看 create view v1 as select ...查询语句WITH ...
- v-cloak
v-cloak 不需要表达式 用法: 这个指令保持在元素上直到关联实例结束编译.和 CSS 规则如 [v-cloak] { display: none } 一起用时,这个指令可以隐藏未编译的 Must ...
- 近年来爆发的CVE漏洞编号
1.Office漏洞 Office漏洞是大部分APT组织最喜爱的漏洞,Office在个人办公电脑使用量大,对针对性目标是最佳的外网入口,效果也是最直接的. CVE编号 漏洞类型 使用组织 CVE-2 ...
- 【Python 2 到 3 系列】 此整型非彼整型
v2.2 (2.x)以后,python支持不会溢出的 long 型. v3.0后,确切的讲, int 型(依赖运行环境C编译器中long型的精度)消失了,long型替代 int 型,成为新的.不依赖运 ...
- JZOJ 3521. 道路覆盖
Description ar把一段凹凸不平的路分成了高度不同的N段,并用H[i]表示第i段高度.现在Tar一共有n种泥土可用,它们都能覆盖给定的连续的k个部分. 对于第i种泥土,它的价格为C[i],可 ...
- ubuntu安装tomcat7
1. 下载apache-tomcat-7.0.64.tar.gz 进入tomcat官网:http://tomcat.apache.org/download-70.cgi下载相应的压缩包: 2. 上传安 ...
- 状压DP详解(位运算)
前言: 状压DP是一种非常暴力的做法(有一些可以排除某些状态的除外),例如dp[S][v]中,S可以代表已经访问过的顶点的集合,v可以代表当前所在的顶点为v.S代表的就是一种状态(二进制表示),比如 ...
- python搭建友盟以及个推推送web服务器
一.友盟客户端demo: 由于SDK原因,新版Android Studio的Android API 28 Platform无法同步新建项目, 所以我最终选择下载android-studio-bundl ...
- 常用 Git 命令清单【转--阮一峰】
常用 Git 命令清单 感谢作者 --> 原文链接 我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下 ...