http://poj.org/problem?id=1179

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
/**
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(记忆化搜索写法)有巨坑!的更多相关文章

  1. (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)

    http://poj.org/problem?id=3186   Description FJ has purchased N (1 <= N <= 2000) yummy treats ...

  2. uva 10891 区间dp+记忆化搜索

    https://vjudge.net/problem/UVA-10891 给定一个序列x,A和B依次取数,规则是每次只能从头或者尾部取走若干个数,A和B采取的策略使得自己取出的数尽量和最大,A是先手, ...

  3. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  4. loj 1031(区间dp+记忆化搜索)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...

  5. BZOJ1055[HAOI2008]玩具取名 【区间dp + 记忆化搜索】

    题目 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后 他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够 ...

  6. HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索

    题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析:  枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...

  7. hdu 4597 Play Game(区间dp,记忆化搜索)

    Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card ...

  8. poj 1088 滑雪(区间dp+记忆化搜索)

    题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...

  9. Ural 1183 Brackets Sequence(区间DP+记忆化搜索)

    题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...

  10. 洛谷1880 区间dp+记忆化搜索 合并石子

    题目网址:https://www.luogu.com.cn/problem/P1880 题意是:给定一个序列,最小规则是相邻两个值的合并,开销是他们的和,将整个序列合并成一个值的情况下,求解该值的最小 ...

随机推荐

  1. MySQL 数据的增删改查

    一.数据库的增删改 一. 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 1.使用INSERT实现数据的插入 2.UPDATE实现数据的更新 3.使用DELETE实现数 ...

  2. 前端面试基础-html篇之H5新特性

    h5的新特性(目前个人所了解)如下 语义化标签 表单新特性 视频(video)和音频(audio) canvas画布 svg绘图 地理定位 为鼠标提供的拖放API webworker (重点)Stor ...

  3. 启动MyEclipse 出现java.lang.RuntimeException: No application id has been found 解决办法

    咋一看,太熟悉了,就去eclipse\links 目录下,发现指定的MyEclipse的路径不对. 突然想起来了,MyEclipse是换地了. MyEclipse里面内置的eclipse找不到MyEc ...

  4. session 存入redis 或 memcache 的方法

      Session简介 session,中文经常翻译为会话,其本来的含义是 指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个session.有时候我们 ...

  5. 推荐系统入门:作为Rank系统的推荐系统(协同过滤)

    知乎:如何学习推荐系统? 知乎:协同过滤和基于内容的推荐有什么区别? 案例:推荐系统实战?  数据准备:实现推荐栏位:重构接口:后续优化. 简书:实现实时推荐系统的三种方式?基于聚类和协同过滤:基于S ...

  6. 机器学习:随机森林RF-OBB袋外错误率

    文章讲解比较详细,且有Python代码,可以作为有用的参考. 原文链接:http://blog.csdn.net/zhufenglonglove/article/details/51785220  参 ...

  7. 【技术累积】【点】【sql】【15】MySQL的TEXT和SELECT问题

    说明 只是TEXT和SELECT两个东西相关的问题,并不是两者之间的关系. TEXT TEXT类型,大文本类型,细分起来还有BIGTEXT,TINYTEXT等: 总体而言,就是处理mysql中存储大文 ...

  8. 10--C++多态

    C++多态 C++多态技术 摘要 本文描述了C++中的各种多态性.重点阐述了面向对象的动态多态和基于模板的静态多态,并初步探讨了两种技术的结合使用. 关键词 多态  继续  虚函数  模板  宏  函 ...

  9. ABP(http://www.aspnetboilerplate.com/)下载初始化

    官网:http://www.aspnetboilerplate.com/ 下载 下载完成后用vs2015打开,是2015,低版本打开可能会出现一些问题 生成项目,Nuget会自动下载需要的类库 ABP ...

  10. .Net Core 中X509Certificate2 私钥保存为 pem 的方法

    在自己签发CA证书和颁发X509证书时,私钥通过下面的方法保存为PEM 相关代码可以已经提交在了 https://github.com/q2g/q2g-helper-pem-nuget/pull/13 ...