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.

输入输出样例

输入样例#1:
复制

4
t -7 t 4 x 2 x 5
输出样例#1: 复制

33
1 2

题解

首先是对于环形问题的惯用套路——断环为链,再复制一遍。

对于链上的每个点,我们记录该点的初始值$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的更多相关文章

  1. 「kuangbin带你飞」专题二十二 区间DP

    layout: post title: 「kuangbin带你飞」专题二十二 区间DP author: "luowentaoaa" catalog: true tags: - ku ...

  2. 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp

    「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...

  3. 「CQOI2007」「BZOJ1260」涂色paint (区间dp

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 2057  Solved: 1267[Submit][St ...

  4. IOI1998 Polygon [区间dp]

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

  5. 【IOI1998】Polygon 区间DP

    题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条边 ...

  6. [IOI1998] Polygon (区间dp,和石子合并很相似)

    题意: 给你一个多边形(可以看作n个顶点,n-1条边的图),每一条边上有一个符号(+号或者*号),这个多边形有n个顶点,每一个顶点有一个值 最初你可以把一条边删除掉,这个时候这就是一个n个顶点,n-2 ...

  7. POJ 1179 - Polygon - [区间DP]

    题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a ...

  8. IOI 98 (POJ 1179)Polygon(区间DP)

    很容易想到枚举第一步切掉的边,然后再计算能够产生的最大值. 联想到区间DP,令dp[i][l][r]为第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值是多少. 但是状态不好转移,因为操 ...

  9. 「LuoguP1430」 序列取数(区间dp

    题目描述 给定一个长为n的整数序列(n<=1000),由A和B轮流取数(A先取).每个人可从序列的左端或右端取若干个数(至少一个),但不能两端都取.所有数都被取走后,两人分别统计所取数的和作为各 ...

  10. 「THUSC 2016」成绩单 & 方块消除 (区间dp)

    成绩单 $f[l][r][mi][mx]$表示从l到r发到还没发的部分的最小值为mi最大值为mx时的最小代价. $f[l][r][0][0]$表示从l到r全部发完的代价. 自己写的无脑dp,枚举中转点 ...

随机推荐

  1. C中的继承和多态

    昨天同学面试被问到这个问题,很有水平,以前都没有遇到过这个问题,一时自己也不知道怎么回答. 网上学习了一下,记录以备后用! C/C++ Internals : 里面的问题都写的不错,可以读读! Ref ...

  2. host dig nslookup bind

    这三个工具包含在yum install bind-utils -y dig -t mx|ns|A baidu.com qq.com dig -x 113,11.2.11 http://www.cnbl ...

  3. PNG24图片兼容IE6解决的方法

    非常多人都遇到一个问题:那就是PNG不能正常显示,比方: 网上试过的非常多办法都非常难实现.要嘛就是效果不好,那如今最好的办法就是直接调用JS插件,解决! 点击下载 如今说一下怎么用这个文件吧! 首先 ...

  4. Lightoj 1088 - Points in Segments 【二分】

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个. 思路:寻 ...

  5. 《UNIX-Shell编程24学时教程》读书笔记Chap3,4 文件,目录操作

    Chap3 文件操作   P28 在这章中,要着重记住一些常用的选项,要有使用正则表达式的思维,能更快达到目的.----@im天行 3.1 列文件名 .profile  sh的初始化脚本: .kshr ...

  6. ZYThumbnailTableView---堪比一个小型阅读App

    Demo github地址: https://github.com/liuzhiyi1992/ZYThumbnailTableView 原文地址:http://zyden.vicp.cc/zythum ...

  7. 从主机给VM Copy文件

    不能从主机给VM 复制文件,在VM里,setting --->option--->shareFolder-->enable-->add(要share的文件路径) then=== ...

  8. Fighting regressions with git bisect---within git bisect algorithm

    https://www.kernel.org/pub/software/scm/git/docs/git-bisect-lk2009.html Fighting regressions with gi ...

  9. Git bare repo with multiple branches

    http://stackoverflow.com/questions/9324762/git-bare-repo-with-multiple-branches Q: I want to make a ...

  10. 服务管理-Apache

    WEB服务器介绍 web server 有两个意思: 一台负责提供网页的服务器,通过HTTP协议传给客户端(一般是指网页浏览器). 一个提供网页的服务器程序. 常见的WEB服务器 Apache是世界使 ...