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. iOS开发 frame 与 bounds 的区别与关系 转自隔叶黄莺

    frame和bounds是UIView中的两个属性(property). frame指的是:该view在父view坐标系统中的位置和大小.(参照点是父亲的坐标系统) bounds指的是:该view在本 ...

  2. VSCode------.net core2.0发布后配置到Window Service 2008R2报500错误

    如图: 解决方法: 出现这个错误是因为 IIS 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改. 要取消锁定可以运行命令行 %windir%\system32\ine ...

  3. Java位运算加密

    创建一个类,通过位运算中的”^"异或运算符把字符串与一个指定的值进行异或运算,从而改变字符串每个字符的值,这样就可以得到一个加密后的字符串.当把加密后的字符串作为程序输入内容,再与那个指定的 ...

  4. C语言对文件的基本操作

    在C语言中,对于文件的操作是利用FILE结构体进行的. 几个常用的操作文件函数简介 1:打开文件 FILE *fopen( const char *filename, const char *mode ...

  5. iOS NSURLSession VS NSURLConnection

    NSURLSession VS NSURLConnection NSURLSession可以看做是NSURLConnection的进化版,其对NSURLConnection的改进点有: * 根据每个S ...

  6. linux-nohup后台运行

    先说一下linux重定向: 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出. 在一般使用时,默认的是标准输出,既1.当我们需要特殊用途时,可以使用其他标 ...

  7. java-使用SImpleDateFormat格式化时间输出

    之前有篇博客是使用NSDateFormatter来对时间进行格式化输出,但使用起来有点繁琐,今天介绍下最近刚刚使用的SimpleDateFormat. public class SimpleDateF ...

  8. React Native(十三)——ios键盘挡住textInput

    渐入佳境 用React Native重构的项目也快接近尾声,剩下的就是适配ios的功能了.慢慢地也从中琢磨出了一点门道,于是就遇见了键盘遮挡textInput问题斑斑: 正常页面: android点击 ...

  9. 对C#中几个循环语句的使用,请教

    今天是在云和数据学院学习的第四天,由于各种原因···今天自己预习的循环语句的用法以及写了几个程序,也遇到各种的问题了···纠结.由于还是在学习的很初初初级,所以好多简单的方法还是不知道怎么写出来,只得 ...

  10. Python进阶 学习笔记(一)

    (笔记范围:第一章 课程介绍:第二章 函数式编程:第三章 模块) Python支持的函数式编程 不是纯函数式编程:允许有变量 支持高阶函数:函数也可以作为变量传入 支持闭包:有了闭包就能返回函数 有限 ...