Polygon

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 5293 Accepted: 2238

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

题意应该不难读懂吧,这里我就不赘述了。我只想谈谈这个题目给我带来的收获,和这个题目的状态转移方程。首先这个是一道什么样的DP题目呢?在写这道题目之前,应该知道一个概念就是最优矩阵链乘,就是连续几个矩阵相乘,由于矩阵相乘满足结合率,所以可以选取不同的想乘顺序,但是不同的顺序会带来不同的计算次数,因为矩阵相乘的特性,行数乘以列数什么乱七八糟的,可以百度一下。这个我都忘了,以前线代课的时候记得很清楚。然后这道题目差不多同一个道理,就是怎么样的顺序安排,使得最后得到的值最大。利用动态规划的思想,如果在去掉一条边的情况下,那么最终的情况肯定是这条边的一个点到通过其他点到另一个点,这个记做DP[d%n][(d+n)%n],无论你先选择操作哪条边,最终的结果肯定是从d到d+n,这个区间,计算的结果。由于你可以选择顺序,所以d到d+n,你就可以分开进行,比如先算k到d+n,再算d到k,那么k就是分割点

dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);

我知道我说了一大堆,可能你什么都不懂,因为动态规划是很难用语言描述准确的,只知道它的意思。

这里还有一点就是,dp维护的不仅是最大值,还要最小值,因为负负得正啊,所以最小值也要考虑!!!

#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h> using namespace std;
#define MAX 999999
int dmax[55][55];
int dmin[55][55];
int n;
char c[55];
int a[55];
int res[55];
int main()
{
int tot;
int ans;
int ans1,ans2;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
dmax[i][j]=-MAX;
dmin[i][j]=MAX;
}
}
tot=0;
ans=-MAX;
getchar();
for(int i=0;i<n;i++)
{
cin>>c[i]>>a[i];
dmax[i][i]=dmin[i][i]=a[i]; } for(int d=0;d<n;d++)
{
for(int i=1;i<n;i++)
{ for(int j=d;j+i<d+n;j++)
{ // if(dmax[j%n][(j+i)%n]!=-MAX)
// continue; for(int k=j;k<j+i;k++)
{
if(c[(k+1)%n]=='t')
{
dmax[j%n][(j+i)%n]=max(dmax[j%n][(j+i)%n],dmax[j%n][k%n]+dmax[(k+1)%n][(j+i)%n]);
dmin[j%n][(j+i)%n]=min(dmin[j%n][(j+i)%n],dmin[j%n][k%n]+dmin[(k+1)%n][(j+i)%n]);
}
else
{
dmax[j%n][(j+i)%n]=max(dmax[j%n][(j+i)%n],dmax[j%n][k%n]*dmax[(k+1)%n][(j+i)%n]);
dmax[j%n][(j+i)%n]=max(dmax[j%n][(j+i)%n],dmin[j%n][k%n]*dmin[(k+1)%n][(j+i)%n]);
dmin[j%n][(j+i)%n]=min(dmin[j%n][(j+i)%n],dmax[j%n][k%n]*dmin[(k+1)%n][(j+i)%n]);
dmin[j%n][(j+i)%n]=min(dmin[j%n][(j+i)%n],dmin[j%n][k%n]*dmax[(k+1)%n][(j+i)%n]);
}
} } }
if(dmax[d][(d+n-1)%n]>ans)
{
tot=0;
res[tot++]=d+1;
ans=dmax[d][(d+n-1)%n];
}
else if(dmax[d][(d+n-1)%n]==ans)
{
res[tot++]=d+1;
} }
printf("%d\n",ans);
for(int i=0;i<tot;i++)
{
if(i!=tot-1)
printf("%d ",res[i]);
else
printf("%d\n",res[i]);
} }
return 0; }

POJ-1179 Polygon (动态规划)的更多相关文章

  1. poj 1179 Polygon

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

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

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

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

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

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

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

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

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

  6. poj 3783 Balls 动态规划 100层楼投鸡蛋问题

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...

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

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

  8. POJ 1179 IOI1998 Polygon

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

  9. 【POJ 1179】Polygon

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

  10. POJ 3597 Polygon Division 多边形剖分

    题目链接: http://poj.org/problem?id=3597 Polygon Division Time Limit: 2000MSMemory Limit: 131072K 问题描述 G ...

随机推荐

  1. AngularJS------报错"The selector "app-user-item" did not match any elements"

    原因:新建的组件没有在任何界面使用到 解决方法:在界面使用该组件

  2. org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'parentId' in 'class java.lang.String'

    Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ' ...

  3. 不用数据线连接到Android手机进行调试

    这两天USB线丢了,老是找同事借也不方便,于是就网上找各种方法,这里总结个最简单的,当然你的手机需要root: 1 要打开WIFI,手机要和电脑在同一局域网内,这个你可以使用你的开发机共享wifi即可 ...

  4. Struts2_day03讲义_使用Struts2完成对客户查询的优化操作

  5. 由于OBJ模型的读取引起的Release无问题Debug卡死问题

    有些时候会遇到Release版本正常运行,但是Debug无法运行甚至崩溃,原因有很多种,这里记录一下由于模型文件读取引起的Debug问题. 项目中需要读取一个obj模型文件,30M左右,Debug模式 ...

  6. mybatis 之 parameterType="String" resultType="java.util.HashMap">

    public ServiceMessage<Map<String, String>> getGoodsStockNo( List<Map<String, Strin ...

  7. React Native(三)——推送jpush-react-native

    瞬间,有种满血复活的赶脚…… 原因呢,就是熟悉了rn项目的套路:当老大问道,“推送功能看了还是没看呢?”的时候,虽然一直没有调试通,但还是不怯场的回答,“看了,按照网上说的也配了,但是还是用不了,不知 ...

  8. 《转载》Python3安装Scrapy

    运行平台:Windows Python版本:Python3.x IDE:Sublime text3 转载自:http://blog.csdn.net/c406495762/article/detail ...

  9. PHP错误 。Parse error: syntax error, unexpected T_INLINE_HTML, expecting T_ENDSWITCH or T_CASE or T_DEFAULT

    If you wan't to use the alternative syntax for switch statements this won't work: <div> <?p ...

  10. Win8设计——现代设计,可使你的应用脱颖而出的元素

    Microsoft 设计准则 Windows 在现代设计方面遥遥领先.它采用了“真实数字”原则并从瑞士风格和交通枢纽的寻路系统中汲取灵感. 阅读详细信息 设计元素 动态磁贴 动态磁贴向你提供了一个独特 ...