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. cmd.exe启动参数详解

    https://blog.csdn.net/moonhillcity/article/details/53039763 各个系统中打开文件的命令 "windows系统: cmd " ...

  2. [Scikit-learn] 2.5 Dimensionality reduction - ICA

    理论学习: 独立成分分析ICA历史 Ref: Lecture 15 | Machine Learning (Stanford) - NG From: https://wenku.baidu.com/v ...

  3. SpringBoot------集成PageHelper分页功能

    添加MyBatis的代码,地址 https://www.cnblogs.com/tianhengblogs/p/9537665.html 修改以下部分: 1.添加MyBatisConfig packa ...

  4. spring定时任务详解(@Scheduled注解)多线程讲解

    (一)在xml里加入task的命名空间 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  5. my-small.ini、my-medium.ini、my-large.ini、my-huge.ini文件的作用

    安装完mysql之后或者是下载的免安装版解压之后,默认是没有my.ini文件的.但是,有几个类似的文件,如my-small.ini.my-medium.ini.my-large.ini.my-huge ...

  6. 【代码审计】BootCMS v1.1.3 文件上传漏洞分析

      0x00 环境准备 BootCMS官网:http://www.kilofox.net 网站源码版本:BootCMS v1.1.3  发布日期:2016年10月17日 程序源码下载:http://w ...

  7. 手把手让你实现开源企业级web高并发解决方案(lvs+heartbeat+varnish+nginx+eAccelerator+memcached)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freeze.blog.51cto.com/1846439/677348 此文凝聚 ...

  8. 执行Batch批处理遇到的问题

    1.务必关掉自动提交 增强执行效率 conn.setAutoCommit(false); 2.executeBatch失效问题 <1>务必将语句pstmt = conn.prepareSt ...

  9. php的session问题总结

    1. 看文档发现,在session的configure option中有三个关于gc的,分别是: session.gc_probability "1" PHP_INI_ALL se ...

  10. django进阶-小实例

    前言: 这篇博客对上篇博客django进阶作下补充. 一.效果图 前端界面较简单(丑),有两个功能: 从数据库中取出书名 eg: 新书A 在form表单输入书名,选择出版社,选择作者(多选),输入完毕 ...