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. 使用TypeDescriptor给类动态添加Attribute【转】

    源文 : http://www.cnblogs.com/bicker/p/3326763.html 给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overf ...

  2. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  3. [转]eclipse查看某个java类属于哪个jar包

    原文地址:https://blog.csdn.net/csdnliuxin123524/article/details/73572836 在eclipse界面直接按ctrl+shift+t,弹出以下界 ...

  4. 【课程笔记】比特币和数字货币技术[Bitcoin and Cryptocurrency Technologies] week1

    源地址(可能要FQ):https://www.coursera.org/learn/cryptocurrency/home/welcome 1.1 Cryptographic Hash Functio ...

  5. ubuntu 14.04 下利用apt-get方式安装opencv

    转载,请注明出处:http://blog.csdn.net/tina_ttl 目录(?)[+] 标签(空格分隔): Linux学习 OpenCV ubuntu 1404 下利用apt-get方式安装O ...

  6. MongoDB的一些操作技巧

    去年三月底入职上海的一家互联网公司,由于项目使用的是MongoDB数据库所以有机会接触了MongoDB.在项目的开发过程中使用系统原有的一些方法查询MongoDB感觉很费力,用起来也不爽,所以私下里就 ...

  7. VB.NET版机房收费系统—数据库设计

    之前第一遍机房收费的时候,用的数据库是别人的.认知也仅仅能建立在别人的基础上,等自考中<数据库系统原理>这本书学完了之后,再去看曾经的数据库,发现数据库真的还须要进一步的优化.以下是我设计 ...

  8. netty+Protobuf (整合一)

    netty+Protobuf 整合实战 疯狂创客圈 死磕Netty 亿级流量架构系列之12 [博客园 总入口 ] 本文说明 本篇是 netty+Protobuf 整合实战的 第一篇,完成一个 基于Ne ...

  9. The server must be started under an unprivileged user ID to prevent

    mysql8 PostgreSQL [root@test local]# postgres -D /usr/local/pgsql/data"root" execution of ...

  10. 编译性语言&amp;解释性语言

    计算机是不能理解高级语言.当然也就不能直接执行高级语言了.计算机仅仅能直接理解机器语言,所以不论什么语言,都必须将其翻译成机器语言.不论什么编程语言编写的程序归根究竟都是由底层机器的机器代码(01序列 ...