poj1179 区间dp(记忆化搜索写法)有巨坑!
http://poj.org/problem?id=1179
Description
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
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
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
/**
hdu 1179 区间dp(记忆化搜索写法)
题目大意:给定一个n个节点的环,环的每条边代表+或者*。问最開始把哪条边去掉。剩下的做运算能够得到最大的表达式的值
解题思路:枚举去掉n条边的随意一条,然后区间dp来写。值得一提的是两个最小的负数相乘就会是最大的值,所以我们不能仅仅维护最大值
同一时候也须要维护最小值。 坑啊,这个陷阱太厉害了
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std; int num[105],sym[105],n;
int dpmin[155][155],dpmax[155][155];
int vismin[155][155],vismax[155][155];
int MAX(int i,int j);
int MIN(int i,int j); int MAX(int x,int y)
{
if(vismax[x][y])return dpmax[x][y];
vismax[x][y]=1;
if(y==x)
{
dpmax[x][y]=num[x];
return dpmax[x][y];
}
dpmax[x][y]=-1000000007;
for(int i=x;i<y;i++)
{
int l=MAX(x,i);
int ll=MIN(x,i);
int r=MAX(i+1,y);
int rr=MIN(i+1,y);
if(sym[i+1])
{
dpmax[x][y]=max(dpmax[x][y],l+r);
}
else
{
int ans=l*r;
ans=max(ans,l*rr);
ans=max(ans,ll*r);
ans=max(ans,ll*rr);
dpmax[x][y]=max(dpmax[x][y],ans);
}
}
return dpmax[x][y];
} int MIN(int x,int y)
{
if(vismin[x][y])return dpmin[x][y];
vismin[x][y]=1;
if(y==x)
{
dpmin[x][y]=num[x];
return dpmin[x][y];
}
dpmin[x][y]=1000000007;
for(int i=x;i<y;i++)
{
int l=MAX(x,i);
int ll=MIN(x,i);
int r=MAX(i+1,y);
int rr=MIN(i+1,y);
if(sym[i+1])
{
dpmin[x][y]=min(dpmin[x][y],ll+rr);
}
else
{
int ans=l*r;
ans=min(ans,l*rr);
ans=min(ans,ll*r);
ans=min(ans,ll*rr);
dpmin[x][y]=min(dpmin[x][y],ans);
}
}
return dpmin[x][y];
}
int main()
{
while(~scanf("%d%*c",&n))
{
for(int i=0;i<n;i++)
{
char ch;
scanf("%c %d%*c",&ch,&num[i]);
num[i+n]=num[i];
sym[i+n]=sym[i]=(ch=='t');
}
memset(dpmin,0,sizeof(dpmin));
memset(dpmax,0,sizeof(dpmax));
memset(vismin,0,sizeof(vismin));
memset(vismax,0,sizeof(vismax));
int maxx=-1000000007;
int sum[55],id;
for(int i=0;i<n;i++)
{
int ans=MAX(i,i+n-1);
/** for(int j=i;j<=i+n-1;j++)
{
printf("%d %c ",num[j],sym[j+1]==0?'*':'+');
}
printf("\n%d\n",ans);*/
if(ans>maxx)
{
maxx=ans;
id=0;
sum[id++]=i+1;
}
else if(ans==maxx)
{
sum[id++]=i+1;
}
}
printf("%d\n",maxx);
for(int i=0;i<id;i++)
{
printf(i==id-1?"%d\n":"%d ",sum[i]);
}
}
return 0;
}
poj1179 区间dp(记忆化搜索写法)有巨坑!的更多相关文章
- (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
http://poj.org/problem?id=3186 Description FJ has purchased N (1 <= N <= 2000) yummy treats ...
- uva 10891 区间dp+记忆化搜索
https://vjudge.net/problem/UVA-10891 给定一个序列x,A和B依次取数,规则是每次只能从头或者尾部取走若干个数,A和B采取的策略使得自己取出的数尽量和最大,A是先手, ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- loj 1031(区间dp+记忆化搜索)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...
- BZOJ1055[HAOI2008]玩具取名 【区间dp + 记忆化搜索】
题目 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后 他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够 ...
- HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索
题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析: 枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...
- hdu 4597 Play Game(区间dp,记忆化搜索)
Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card ...
- poj 1088 滑雪(区间dp+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...
- Ural 1183 Brackets Sequence(区间DP+记忆化搜索)
题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...
- 洛谷1880 区间dp+记忆化搜索 合并石子
题目网址:https://www.luogu.com.cn/problem/P1880 题意是:给定一个序列,最小规则是相邻两个值的合并,开销是他们的和,将整个序列合并成一个值的情况下,求解该值的最小 ...
随机推荐
- Spark2.0.2+Zeppelin0.6.2 环境搭建 初探
0.抱怨与其他(此部分与标题没有太多联系): 首先一点想说的是版本问题,为什么标题我会写清楚版本号呢!原因就是版本不对真的很会坑人. 就在写这篇博客的同一天,我还写了另一篇,是 Hadoop2.7.3 ...
- Cloudera Manager安装之利用parcels方式(在线或离线)安装单节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(Ubuntu14.04)(四)
.. 欢迎大家,加入我的微信公众号:大数据躺过的坑 免费给分享 同时,大家可以关注我的个人博客: http://www.cnblogs.com/zlslch/ 和 http ...
- MessageDigest 加密和解密2
package com.drawthink.platform.util; import java.security.MessageDigest; import java.security.NoSuch ...
- Android 新浪微博开放平台应用 android签名怎么获得
方法一: 通过命令行,直接生成MD5值 keytool -list -v -keystore keystorefile -storepass 其中keytool为jdk自带工具:keystorefil ...
- dubbo之结果缓存
结果缓存,用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用户加缓存的工作量. lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存. threadlocal 当前线程缓存,比如 ...
- spring中的prop、set、list、map
props.set.list.map这些事spring配置文件中很常见的标签,下面说下各自的适用场合. props:用于键值对,建和值都为string类型. <property name=&qu ...
- 10--C++多态
C++多态 C++多态技术 摘要 本文描述了C++中的各种多态性.重点阐述了面向对象的动态多态和基于模板的静态多态,并初步探讨了两种技术的结合使用. 关键词 多态 继续 虚函数 模板 宏 函 ...
- 【sqli-labs】 less23 Error based - strip comments (GET型基于错误的去除注释的注入)
. 加单引号报错 加# http://localhost/sqli-labs-master/Less-23/?id=1'%23 错误没有改变,推测过滤了# 查看源码发现# -- 都被替换掉了 那么可用 ...
- C#遍历/反射 属性/字段
public static string SortParam<T>(T t) { string tStr = string.Empty; if (t == null) { return s ...
- BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级 分层图最短路 + Dijkstra
Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i ...