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. 【scala】 scala xml 处理(⑨)

    1.scala 处理xml 2. 获取属性 3.修改节点 4.遍历 5.模式匹配 6.命名空间 7.文件加载 import scala.xml._ /** * @author xwolf * @sin ...

  2. Dubbo -- 系统学习 笔记 -- 示例 -- 集群容错

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 集群容错 在集群调用失败时,Dubbo提供了多种容错方案,缺省为failover重 ...

  3. Python中的类(上)

    在Python中,可以通过class关键字定义自己的类,然后通过自定义的类对象类创建实例对象. 例如,下面创建了一个Student的类,并且实现了这个类的初始化函数"__init__&quo ...

  4. iOS 播放gif动态图的方式探讨

    原文链接:http://my.oschina.net/u/2340880/blog/608560 摘要iOS中没有现成的接口来展示gif动态图,但可以通过其他的方式来处理gif图的展示.iOS中播放g ...

  5. GreenPlum数据库安装

    第一章    文档概述 本文描述适用于Greenplum4.0以上版本的安装操作.所涉及到的操作系统相关参数调整,主要针对Redhat Linux操作系统. 第二章    安装介质 操作系统:Cent ...

  6. N76E003之SPI

    串行外围总线 (SPI)N76E003系列提供支持高速串行通信的SPI模块.SPI 为微控制与外设 EEPROM, LCD 驱动, D/A 转换之间提供全双工.高速.同步传输的总线.可提供主机从机模式 ...

  7. 【LeetCode OJ】Median of Two Sorted Arrays

    题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...

  8. iOS - UICollectionView 瀑布流 添加表头视图的坑

    UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...

  9. 关于RabbitMQ交换机的理解

    RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.消息中间件主要用于组件之间的解耦,消 ...

  10. Malab 常用数学函数

    l        三角函数和双曲函数 名称 含义 名称 含义 名称 含义 sin 正弦 csc 余割 atanh 反双曲正切 cos 余弦 asec 反正割 acoth 反双曲余切 tan 正切 ac ...