题目链接:http://poj.org/problem?id=1179

Time Limit: 1000MS Memory Limit: 10000K

Description

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. 

Input

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]. 

Output

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.

Sample Input

4
t -7 t 4 x 2 x 5

Sample Output

33
1 2

题意:

给出一个由无向边和节点组成的环,每个节点上有一个数字,每条边上有一个运算符(加或乘),

现在先割断一条边,然后环就成为一个链,然后你每次可以将这条链上的一条边缩成一个点,产生的新点的权值就是两个节点配合边运算所产生的结果。

不停地缩边成点,直到最后只有一个点为止,求这个点的权值最大是多少。

并给出所有能产生这个最大值的首先割断的边的编号,要求从小到大输出。

题解:

区间DP,周赛上wyb出的毒瘤题,每次两个小区间合并的时候,要记得有可能两个最小的负数相乘可能会产生正数最大值。

因此需要同时维护区间最小值和最大值。

AC代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> pii;
const int INF=0x3f3f3f3f;
const int maxn=; int n;
int op[*maxn],nm[*maxn];
pii dp[*maxn][*maxn];
inline int calc(int type,int a,int b){return type?a*b:a+b;}
inline void updatemn(int &x,int y){if(x>y) x=y;}
int solve(int l,int r)
{
for(int s=;s<=r-l+;s++)
{
for(int st=l,ed=st+s-;ed<=r;st++,ed++)
{
dp[st][ed].first=-INF;
dp[st][ed].second=INF;
for(int mid=st+;mid<=ed;mid++)
{
pii le=dp[st][mid-];
pii ri=dp[mid][ed]; int tmp1=calc(op[mid],le.first,ri.first);
dp[st][ed].first=max(dp[st][ed].first,tmp1);
dp[st][ed].second=min(dp[st][ed].second,tmp1); int tmp2=calc(op[mid],le.first,ri.second);
dp[st][ed].first=max(dp[st][ed].first,tmp2);
dp[st][ed].second=min(dp[st][ed].second,tmp2); int tmp3=calc(op[mid],le.second,ri.first);
dp[st][ed].first=max(dp[st][ed].first,tmp3);
dp[st][ed].second=min(dp[st][ed].second,tmp3); int tmp4=calc(op[mid],le.second,ri.second);
dp[st][ed].first=max(dp[st][ed].first,tmp4);
dp[st][ed].second=min(dp[st][ed].second,tmp4);
}
}
}
return dp[l][r].first;
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
int m; char o[];
scanf("%s",o); op[i]=op[n+i]=(o[]=='x');
scanf("%d",&m); nm[i]=nm[n+i]=m;
} for(int i=;i<*n;i++) dp[i][i]=make_pair(nm[i%n],nm[i%n]);
int ans=-INF;
for(int c=;c<n;c++) ans=max(ans,solve(c,c+n-)); vector<int> E;
for(int c=;c<n;c++) if(dp[c][c+n-].first==ans) E.push_back(c+);
sort(E.begin(),E.end());
printf("%d\n",ans);
for(int i=;i<E.size();i++) printf("%d%c",E[i],(i==E.size()-)?'\n':' ');
}

数据:

x  x  t  t  x 

x - t - t - t - x -

t  x  t -

x  t  x  t  t  x  x  x  x  x  x  t  t  x  t  x  x  t  x  x  t  x  x  x  x  x  t  x  x  x 

x  x  x  x - t  x - x - x  t  t - x  t  x  x  t  x  x - x - x  x  t  x  t  x  x  x  t  x  x  x  x  x  x  x - t  x  x - x - t  x  t  x  x  x - t  t - t - x 

POJ 1179 - Polygon - [区间DP]的更多相关文章

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

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

  2. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  3. poj 1179 Polygon

    http://poj.org/problem?id=1179 Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  4. POJ 1160 经典区间dp/四边形优化

    链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...

  5. IOI1998 Polygon [区间dp]

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

  6. POJ 1390 Blocks(区间DP)

    Blocks [题目链接]Blocks [题目类型]区间DP &题意: 给定n个不同颜色的盒子,连续的相同颜色的k个盒子可以拿走,权值为k*k,求把所有盒子拿完的最大权值 &题解: 这 ...

  7. poj 2955"Brackets"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...

  8. POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Desc ...

  9. HOJ 1936&POJ 2955 Brackets(区间DP)

    Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...

随机推荐

  1. redis性能测试报告

    服务器配置:16核心,64G 250个并发读:250个并发写性能[内容8千byte] 163为读:164为写:

  2. PureFTP被动端口设置

    修改Pureftp的配置文件把 # PassivePortRange          30000 50000 把前面的#删除 重启pureftpd 注意把被动端口防火墙例外 如果是阿里云主机 安全规 ...

  3. 开源CFD并非万金油

    今天有网友在群里讨论开发CFD软件的事情,众说纷纭,有网友提到"没有必要开发CFD软件了,直接使用开源OpenFOAM就行".但个人认为这说法还是有一些需要商榷的地方,开源软件也不 ...

  4. IIS发布的网站常见的问题汇总

    1.安装.NET4.0时缺少WIC导致不能装上.NET4.0,下载安装后即可,下载地址如下,根据系统版本选择对应软件 32位版:https://www.microsoft.com/zh-cn/down ...

  5. 【Linux高级驱动】I2C驱动框架分析

    1.i2c-dev.c(i2c设备驱动组件层) 功能:1)给用户提供接口 i2c_dev_init  //入口函数 /*申请主设备号*/ register_chrdev(I2C_MAJOR(), &q ...

  6. 按enter键触发登录事件

    $(document).keydown(function(event){ if(event.keyCode==13){ $(".submit").click(); } });

  7. ubuntu12.04安装Docker

    由于公司的虚拟机上的ubuntu都是12.04的,所以要在ubuntu12.04上安装Docker.Docker目前只能运行在64位的机器上面. 先要升级内核 sudo apt-get update ...

  8. Ubuntu下的Wine&WineQQ

    一.安装Wine 1.添加PPA sudo add-apt-repository ppa:ubuntu-wine/ppa 2.更新列表 sudo apt-get update 3.安装Wine sud ...

  9. js调用winform程序(带参数)

    我们会发现,我们点击迅雷下载的时候  网页可以调用应用程序,而且连接会传入迅雷,这个是怎么做到的呢? 原理: 先注册表中添加软件的具体信息,然后通过 href 可以直接调用 1.写入注册表信息,注册, ...

  10. FDDI即光纤分布式数据接口

    光纤分布式数据接口它是于80年代中期发展起来一项局域网技术,它提供的高速数据通信能力要高于当时的以太网(10Mbps)和令牌网(4或16Mbps)的能力.FDDI标准由ANSI X3T9.5标准委员会 ...