POJ 1179 - Polygon - [区间DP]
题目链接:http://poj.org/problem?id=1179
Time Limit: 1000MS Memory Limit: 10000K
Description

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
3 <= N <= 50
For any sequence of moves, vertex labels are in the range [-32768,32767].
Output
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]的更多相关文章
- IOI 98 (POJ 1179)Polygon(区间DP)
很容易想到枚举第一步切掉的边,然后再计算能够产生的最大值. 联想到区间DP,令dp[i][l][r]为第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值是多少. 但是状态不好转移,因为操 ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- poj 1179 Polygon
http://poj.org/problem?id=1179 Polygon Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1160 经典区间dp/四边形优化
链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...
- IOI1998 Polygon [区间dp]
[IOI1998]Polygon 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘 ...
- POJ 1390 Blocks(区间DP)
Blocks [题目链接]Blocks [题目类型]区间DP &题意: 给定n个不同颜色的盒子,连续的相同颜色的k个盒子可以拿走,权值为k*k,求把所有盒子拿完的最大权值 &题解: 这 ...
- poj 2955"Brackets"(区间DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
随机推荐
- 阿里云k8s应用最新日志采集不到的问题
问题描述: 阿里云k8s应用日志之前一直都是可以正常的采集, 先出现一问题, 通过kibana 和阿里云的日志服务都没法展示最新的k8s应用的日志, 部分应用的最新日志有被采集到,但大部分应用日志没有 ...
- 《STL源码剖析》学习之traits编程
侯捷老师在<STL源码剖析>中说到:了解traits编程技术,就像获得“芝麻开门”的口诀一样,从此得以一窥STL源码的奥秘.如此一说,其重要性就不言而喻了. 之前已经介绍过迭代器 ...
- Docker(二):Registry 镜像仓库
- WPF:理解ContentControl——动态添加控件和查找控件
WPF:理解ContentControl--动态添加控件和查找控件 我认为WPF的核心改变之一就是控件模型发生了重要的变化,大的方面说,现在窗口中的控件(大部分)都没有独立的Hwnd了.而且控件可以通 ...
- H5的Video事件,控制方法,及监听
1.标签基本属性 src :视频的属性 poster:视频封面,没有播放时显示的图片preload:预加载autoplay:自动播放loop:循环播放controls:浏览器自带的控制条width:视 ...
- Arrays.asList中所遇到的坑
前言 最近在项目上线的时候发现一个问题,从后台报错日志看:java.lang.UnsupportedOperationException异常 从代码定位来看,原来是使用了Arrays.asList() ...
- Socket网络编程--网络爬虫(3)
上一小节我们实现了从博客园的首页获取一些用户的用户名,并保存起来.接下来的这一小节我将对每个用户名构建一个用户的博客主页,然后从这个主页获取所有能获取到的网页,网页的格式现在是http://www.c ...
- Socket网络编程--简单Web服务器(3)
上一小节已经实现了浏览器发送请求,然后服务器给出应答信息,然后浏览器显示出服务器发送过来的网页.一切看起来都是那么的美好.这一小节就准备实现可以根据地址栏url的不同来返回指定的网页.目前还不考虑带参 ...
- RelativeLayout 布局参数
今天调布局的时候 想把界面做成横屏竖屏都可以的 突然发现之前理解的android:布局参数都是有问题的 今天贴出来 下次自己也记得 以下大部为用在RelativeLayout中的一些参数: andro ...
- MyBean通用报表插件介绍
特性: 1.基于MyBean插件平台.可以在任何插件中无缝调用显示. 2.其他窗体中无需引用报表控件.就可以拥有报表的设计预览打印等功能. 3.甚至可以不用带包,制作报表插件,也就是说你可以将RM的报 ...