Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5472   Accepted: 2334

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

Source

大概就是一个比较简单的dp,枚举中间断开的位置,唯一注意的就是要维护两个值最大值和最小值,因为存在一种情况最小值*最小值反而负负得正了。
 #include <iostream>
#include <cstring>
#include <cstdio>
const int N = + ;
using namespace std ;
int n,a[N];
char ss[N];
long long f[N][N],g[N][N],ans; void Init()
{
scanf("%d",&n);
for(int i = ; i <= n ; ++i)
{
cin>>ss[i]>>a[i];
a[n+i]=a[i],ss[n+i]=ss[i];
}
} void Solve( )
{
for(int i = ; i <= (n<<) ; ++i) for(int j = ; j <= (n<<) ; ++j)f[i][j] = -0x3f3f3f3f ;
for(int i = ; i <= (n<<) ; ++i) for(int j = ; j <= (n<<) ; ++j)g[i][j] = 0x3f3f3f3f ;
for(int i = ; i <= (n<<) ; ++i)
{
if(ss[i+]=='t')
f[i][i+] = g[i][i+] = a[i] + a[i+];
else
f[i][i+] = g[i][i+] = a[i] * a[i+];
f[i][i] = g[i][i] = a[i];
}
long long ans = -0x3f3f3f3f ;
for(int i = (n<<);--i;)
for(int j = i + ; j <=(n<<);++j)
for(int k = i;k<j;++k)
if(ss[k+] == 't')
{
f[i][j] = max(f[i][j],f[i][k]+f[k+][j]);
g[i][j] = min(g[i][j],g[i][k]+g[k+][j]);
}
else
{
long long int a = f[i][k]*g[k+][j],b = f[i][k]*f[k+][j];
long long int c = g[i][k]*f[k+][j],d = g[i][k]*g[k+][j];
f[i][j] = max(f[i][j],max(max(a,b),max(c,d)));
g[i][j] = min(g[i][j],min(min(a,b),min(c,d)));
}
for(int i = ; i<= n ;++i)
ans = max(ans,f[i][i+n-]);
printf("%lld\n",ans);
for(int i=;i<=n;++i)
if(f[i][i+n-] == ans)
printf("%d ",i);
puts("");
} int main( )
{
// freopen("polygon.in","r",stdin);
// freopen("polygon.out","w",stdout);
Init();
Solve();
fclose(stdin);
fclose(stdout);
return ;
}

POJ 1179 IOI1998 Polygon的更多相关文章

  1. 【POJ 1179】Polygon

    [原题链接]传送门 [题解思路] 1.第一感觉没有其他做法,想到动态规划,去环,区间dp 2.f[l,r]表示[l,r]内的最大值,考虑转移 3.最大值分加法和乘法,其中乘法不一定由两个要求合并的区间 ...

  2. DP中环形处理 +(POJ 1179 题解)

    DP中环形处理 对于DP中存在环的情况,大致有两种处理的方法: 对于很多的区间DP来说,很常见的方法就是把原来的环从任意两点断开(注意并不是直接删掉这条边),在复制一条一模一样的链在这条链的后方,当做 ...

  3. IOI1998 Polygon [区间dp]

    [IOI1998]Polygon 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘 ...

  4. Mark一下, dp状态转移方程写对,可是写代码都错,poj 1651 poj 1179

    dp题: 1.写状态转移方程; 2.考虑初始化边界,有意义的赋定值.还没计算的赋边界值: 3.怎么写代码自底向上计算最优值 今天做了几个基础dp,所有是dp方程写对可是初始化以及计算写错 先是poj ...

  5. [IOI1998]Polygon(区间dp)

    [IOI1998]Polygon 题意翻译 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记. 第一步,删除其中一条 ...

  6. poj 1179 Polygon

    http://poj.org/problem?id=1179 Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  7. POJ 1179 - Polygon - [区间DP]

    题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a ...

  8. poj 1179 $Polygon$(断环成链)

    Polygon \(solution:\) upd:还是多讲一下,这道题基本上可以说是一道思维题.一道结论题.一道考验你动态规划基本功是否扎实的题目.因为这道题的数据范围很小,思考一下总能想到断环成链 ...

  9. IOI 98 (POJ 1179)Polygon(区间DP)

    很容易想到枚举第一步切掉的边,然后再计算能够产生的最大值. 联想到区间DP,令dp[i][l][r]为第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值是多少. 但是状态不好转移,因为操 ...

随机推荐

  1. javascript随手记

    编码规范 避免使用全局变量 写在所有函数外面的变量就是全局变量. 之所以要避免使用全局变量是因为:如果有多个类库的话,它们都定义了一个名字的变量.这时候后引入的类库中该变量的值就会覆盖前面引入的类库中 ...

  2. iOS 设置代理过程

    iOS设置代理的过程 (以模拟 button 作用为例) 1.写协议 新建一个名为 MyButton 的文件,继承于 UIView,在该文件里 声明协议 myDelegate 2.写协议方法 为声明的 ...

  3. MyBatis之传入参数——parameterType(转)

    鸣谢:http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 ------------------------------------ ...

  4. 【转】spring3 MVC实战,手工搭建Spring3项目demo

    更新:这几天对spring3的理解又进了一步,今天抽空把这篇文章中的错误和不当之处做了修改. 最近的项目在用Spring3,涉及到了基于注解的MVC,事务管理,与hibernate的整合开发等内容,我 ...

  5. ps 命令使用总结

    ps命令用来查看进程信息,它是类似于快照类型的只显示一次,如果想及时刷新请用top命令. 1. 常用参数列表 -a 显示所有终端机下执行的进程,除了阶段作业领导者之外. a 显示现行终端机下的所有进程 ...

  6. linux下跨服务器文件文件夹的复制

    文件的复制:scp –P (端口号) ./authorized_keys berchina@hadoop002:/home/berchina 文件夹的复制:scp -r -P (端口号) /home/ ...

  7. [转] acmer必看的26个对acm态度

    acmer必看的26个对acm态度   转载自:http://www.cppblog.com/Darren/archive/2009/08/03/92099.html Attempt Keep on ...

  8. zabbix中文配置指南(转)-服务器监控

    一.Zabbix简介 1.1 Zabbix简介 Zabbix是一个企业级的开源分布式监控解决方案,由一个国外的团队持续维护更新,软件可以自由下载使用,运作团队靠提供收费的技术支持赢利.官方网站:htt ...

  9. 【转】Xcode重构功能怎么用我全告诉你

    原文网址:http://www.cocoachina.com/ios/20160127/15097.html 你会经常需要重构你的代码,让它有更好的结构,可读性或者提高可维护性.Xcode作为IDE其 ...

  10. HDU 3427

    DP: According to the meaning of problems,if we check n to m, assume x and y are both solvable,then w ...