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. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

    一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...

  2. java bean转Map

    /** * @author xxxxxxxxxxx * @param object * 待转化类 * @param format自定义转化类型 * @return Map<String,Stri ...

  3. Discuze修改用户名长度限制

    第一步,在网站 uc_client\model 目录下的 user.php文件中,找到如下代码: ? 1 if($len > 15 || $len < 3 || preg_match(&q ...

  4. idea使用maven搭建ssm框架实现登陆商品增删改查

    创建项目->maven->webapp->输入坐标->完成. pom.xml <project xmlns="http://maven.apache.org/P ...

  5. checked、disabled在原生、jquery、vue下不同写法

          以下是原生和jquery <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...

  6. 安卓代码迁移:ActionBarActivity: cannot be resolved to a type

    参考链接:http://stackoverflow.com/questions/18830736/actionbaractivity-cannot-be-resolved-to-a-type in e ...

  7. LINQ to Entities 不识别方法“System.Nullable`1[System.Int32] DiffDays(System.Nullable`1[System.DateTime], System.Nullable`1[System.DateTime])”,因此该方法无法转换为存储表达式。

    解决方案: db.table.Where(m=>System.Data.Objects.EntityFunctions.DiffDays(m.CreateTime, DateTime.Now) ...

  8. js 请求单个文件 并验证扩展名

    function suffix(file_name) { var three=file_name.split("."); ]; return last; } $('#btnSear ...

  9. Linux 之secureCRT连接SSH

    1.登陆linux系统,打开终端命令.输入 rpm -qa |grep ssh 查找当前系统是否已经安装. 2.如果没有安装SSH软件包,可以通过yum  或rpm安装包进行安装. .3.安装好了之后 ...

  10. eas之kdtable格式化

    设置表.列.行或单元的格式化字符串 // 设置表table.getStyleAttributes().setNumberFormat(formatString); // 设置列column.getSt ...