poj1179 Polygon【区间DP】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:6633 | Accepted: 2834 |
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
Source
题意:
给一个n个顶点n条边的多边形,顶点上有一个整数值,边上有一个字符表示+ 或者 *。首先删除一条边,然后每次对两个顶点进行合并,用一个顶点代替这两个顶点,顶点的值是这两个顶点运算的结果,运算符为连接这两个顶点的边。最后只剩下一个顶点,问这个顶点最大值会是多少,以及得到这个结果的删边方法。
思路:
删除了一条边后,就类似于石子合并(https://www.cnblogs.com/wyboooo/p/9757387.html)这道题了。
不同之处在于因为有负数和乘法的存在,最大值有可能是由两个最小值相乘得到的。因此需要同时记录最大值和最小值。【已经遇到好多有负数、乘法要记录最大值最小值的问题了,需要注意!】
最开始需要枚举删掉的边,一个好方法是,将原来的数组在末尾复制一遍。从1~n跑一遍成为枚举,最后找dp[i][i+n-1]的最大值就行了。
这种“任意选择一个位置断开,复制形成2倍长度的链”的方法,是解决DP中环形结构的常用手段之一。
//#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<map> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n;
const int maxn = ;
int num[maxn * ];
char op[maxn * ];
int dp[maxn * ][maxn * ][]; int main()
{
while(scanf("%d", &n) != EOF){ for(int i = ; i <= n; i++){
scanf(" %c %d", &op[i], &num[i]);
}
for(int i = n + ; i <= * n; i++){
op[i] = op[i - n];
num[i] = num[i - n];
}
for(int i = ; i <= n * ; i++){
dp[i][i][] = dp[i][i][] = num[i];
for(int j = i + ; j <= * n; j++){
dp[i][j][] = -inf;
dp[i][j][] = inf;
}
} for(int len = ; len <= n; len++){
for(int l = ; l <= * n - len + ; l++){
int r = l + len - ;
for(int k = l; k < r; k++){ int res1, res2;
if(op[k + ] == 't'){
res1 = dp[l][k][] + dp[k + ][r][];
res2 = dp[l][k][] + dp[k + ][r][];
}
else{
res1 = dp[l][k][] * dp[k + ][r][];
res2 = dp[l][k][] * dp[k + ][r][];
dp[l][r][] = max(dp[l][r][], dp[l][k][] * dp[k + ][r][]);
dp[l][r][] = min(dp[l][r][], dp[l][k][] * dp[k + ][r][]);
dp[l][r][] = min(dp[l][r][], dp[l][k][] * dp[k + ][r][]);
}
dp[l][r][] = max(dp[l][r][], res1);
dp[l][r][] = min(dp[l][r][], res2);
}
}
} int ans = -inf;
for(int i = ; i <= n; i++){
ans = max(dp[i][i + n - ][], ans);
}
printf("%d\n", ans);
bool flag = false;
for(int i = ; i <= n; i++){
if(dp[i][i + n - ][] != ans)continue;
if(flag){
printf(" ");
}
else{
flag = true;
}
printf("%d", i);
}
printf("\n");
}
return ;
}
poj1179 Polygon【区间DP】的更多相关文章
- POJ1179 Polygon 区间DP
题目大意: 多边形游戏,有N个顶点的多边形,3 <= N <= 50 ,多边形有N条边,每个顶点中有一个数字(可正可负),每条边上或者是“+”号,或者是“*”号.边从1到N编号,首先选择一 ...
- POJ 1179 - Polygon - [区间DP]
题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a ...
- IOI1998 Polygon [区间dp]
[IOI1998]Polygon 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘 ...
- IOI 98 (POJ 1179)Polygon(区间DP)
很容易想到枚举第一步切掉的边,然后再计算能够产生的最大值. 联想到区间DP,令dp[i][l][r]为第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值是多少. 但是状态不好转移,因为操 ...
- poj1179多边形——区间DP
题目:http://poj.org/problem?id=1179 区间DP,值得注意的是有负值,而且有乘法,因此可能会影响最大值: 注意memset中写-1仅仅是-1,-2才是一个很小的负数: 最后 ...
- 【IOI1998】Polygon 区间DP
题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条边 ...
- [IOI1998] Polygon (区间dp,和石子合并很相似)
题意: 给你一个多边形(可以看作n个顶点,n-1条边的图),每一条边上有一个符号(+号或者*号),这个多边形有n个顶点,每一个顶点有一个值 最初你可以把一条边删除掉,这个时候这就是一个n个顶点,n-2 ...
- 【POJ1179】Polygon 区间DP
这道题是典型的环形石子归并模型,破环成链后时间复杂度为\(O(n^3)\) 不过,因为题目中所给的数字可能是负数,仅仅记录区间内合并之后的最大值并不满足动态规划的最优子结构性质.因此,还需要额外记录下 ...
- poj1179 环形+区间dp
因为要用到模,所以左起点设置为0比较好 #include<iostream> #include<cstdio> #include<cstring> #define ...
- 「IOI1998」「LuoguP4342」Polygon(区间dp
P4342 [IOI1998]Polygon - 洛谷 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符 ...
随机推荐
- Visual Studio:error MSB8020
状况如下: error MSB8020: The builds tools for v120 (Platform Toolset = 'v120') cannot be found. To build ...
- Eclipse中复制android项目后要改动的地方
1.清单文件中,改package=2.修改包名3.清单文件中app_name F3点进去修改名字
- java生成webservice
使用Eclipse生成一个WebService应用 1.创建一个Dynamic web project 2.创建一个对外提供服务的类.比如: package com.guorui.services; ...
- tftp server setup
今天开始调试ARM的板子,要通过tftp下载到板子上,所以又要配置tftp服务器,真的烦死了… (本人酷爱装系统,所以经常都要搞配置) 因为之前已经在Ubuntu下搭建过很多次tftp服务器了,但是一 ...
- Android——onCreate( )方法详解(转)
android开发之onCreate( )方法详解 onCreate( )方法是android应用程序中最常见的方法之一,那么,我们在使用onCreate()方法的时候应该注意哪些问题呢? 先看看Go ...
- select 5种子句介绍
一.Where 条件查询 ①where expression 用法:expression为真,则该行取出 运用场合 各种条件查询场合,如按学号查学生,按价格查商品,按发布时间查新闻等 ②select ...
- golang 环境bash 以及shell
standard_init_linux.go:178: exec user process caused "no such file or directory" 2018年04月2 ...
- JavaBeans wiki 摘译
20161013最新提示:既然来到这了,为什么不看看 JavaBeans 官方文档学习 ? 鉴于Spring的beans包遵守JavaBean specs,有必要认真研究下JavaBean specs ...
- SSM,即Spring+SpringMVC+MyBatis三个开源框架的整合框架集。
SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...
- 【BZOJ】1621: [Usaco2008 Open]Roads Around The Farm分岔路口(dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1621 这题用笔推一下就懂了的.... 当2|(n-k)时,才能分,否则不能分. 那么dfs即可.. ...