Polygon
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:6633   Accepted: 2834

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

题意:

给一个n个顶点n条边的多边形,顶点上有一个整数值,边上有一个字符表示+ 或者 *。首先删除一条边,然后每次对两个顶点进行合并,用一个顶点代替这两个顶点,顶点的值是这两个顶点运算的结果,运算符为连接这两个顶点的边。最后只剩下一个顶点,问这个顶点最大值会是多少,以及得到这个结果的删边方法。

思路:

删除了一条边后,就类似于石子合并(https://www.cnblogs.com/wyboooo/p/9757387.html)这道题了。

不同之处在于因为有负数和乘法的存在,最大值有可能是由两个最小值相乘得到的。因此需要同时记录最大值和最小值。【已经遇到好多有负数、乘法要记录最大值最小值的问题了,需要注意!】

最开始需要枚举删掉的边,一个好方法是,将原来的数组在末尾复制一遍。从1~n跑一遍成为枚举,最后找dp[i][i+n-1]的最大值就行了。

这种“任意选择一个位置断开,复制形成2倍长度的链”的方法,是解决DP中环形结构的常用手段之一。

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<map> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n;
const int maxn = ;
int num[maxn * ];
char op[maxn * ];
int dp[maxn * ][maxn * ][]; int main()
{
while(scanf("%d", &n) != EOF){ for(int i = ; i <= n; i++){
scanf(" %c %d", &op[i], &num[i]);
}
for(int i = n + ; i <= * n; i++){
op[i] = op[i - n];
num[i] = num[i - n];
}
for(int i = ; i <= n * ; i++){
dp[i][i][] = dp[i][i][] = num[i];
for(int j = i + ; j <= * n; j++){
dp[i][j][] = -inf;
dp[i][j][] = inf;
}
} for(int len = ; len <= n; len++){
for(int l = ; l <= * n - len + ; l++){
int r = l + len - ;
for(int k = l; k < r; k++){ int res1, res2;
if(op[k + ] == 't'){
res1 = dp[l][k][] + dp[k + ][r][];
res2 = dp[l][k][] + dp[k + ][r][];
}
else{
res1 = dp[l][k][] * dp[k + ][r][];
res2 = dp[l][k][] * dp[k + ][r][];
dp[l][r][] = max(dp[l][r][], dp[l][k][] * dp[k + ][r][]);
dp[l][r][] = min(dp[l][r][], dp[l][k][] * dp[k + ][r][]);
dp[l][r][] = min(dp[l][r][], dp[l][k][] * dp[k + ][r][]);
}
dp[l][r][] = max(dp[l][r][], res1);
dp[l][r][] = min(dp[l][r][], res2);
}
}
} int ans = -inf;
for(int i = ; i <= n; i++){
ans = max(dp[i][i + n - ][], ans);
}
printf("%d\n", ans);
bool flag = false;
for(int i = ; i <= n; i++){
if(dp[i][i + n - ][] != ans)continue;
if(flag){
printf(" ");
}
else{
flag = true;
}
printf("%d", i);
}
printf("\n");
}
return ;
}

poj1179 Polygon【区间DP】的更多相关文章

  1. POJ1179 Polygon 区间DP

    题目大意: 多边形游戏,有N个顶点的多边形,3 <= N <= 50 ,多边形有N条边,每个顶点中有一个数字(可正可负),每条边上或者是“+”号,或者是“*”号.边从1到N编号,首先选择一 ...

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

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

  3. IOI1998 Polygon [区间dp]

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

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

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

  5. poj1179多边形——区间DP

    题目:http://poj.org/problem?id=1179 区间DP,值得注意的是有负值,而且有乘法,因此可能会影响最大值: 注意memset中写-1仅仅是-1,-2才是一个很小的负数: 最后 ...

  6. 【IOI1998】Polygon 区间DP

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

  7. [IOI1998] Polygon (区间dp,和石子合并很相似)

    题意: 给你一个多边形(可以看作n个顶点,n-1条边的图),每一条边上有一个符号(+号或者*号),这个多边形有n个顶点,每一个顶点有一个值 最初你可以把一条边删除掉,这个时候这就是一个n个顶点,n-2 ...

  8. 【POJ1179】Polygon 区间DP

    这道题是典型的环形石子归并模型,破环成链后时间复杂度为\(O(n^3)\) 不过,因为题目中所给的数字可能是负数,仅仅记录区间内合并之后的最大值并不满足动态规划的最优子结构性质.因此,还需要额外记录下 ...

  9. poj1179 环形+区间dp

    因为要用到模,所以左起点设置为0比较好 #include<iostream> #include<cstdio> #include<cstring> #define ...

  10. 「IOI1998」「LuoguP4342」Polygon(区间dp

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

随机推荐

  1. windows 和 linux 安装 scrapyd 出现Not a directory site-packages/scrapyd-1.0.1-py2.7.egg/scrapyd/txapp.py

    1  这是因为 scrapyd安装的时候没有 解压 对应的 egg而导致的文件找不到的错误. 2 解决的方法,找到 scrapyd-1.0.1-py2.7.egg 解压缩 里面 有一个  scrapy ...

  2. iOS开发Swift篇—(七)函数

    iOS开发Swift篇—(七)函数 一.函数的定义 (1)函数的定义格式 1 func 函数名(形参列表) -> 返回值类型 { 2 // 函数体... 3 4 } (2)形参列表的格式 形参名 ...

  3. MultipartEntity 乱码

    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Ch ...

  4. FreeRtos——空闲任务与空闲任务钩子函数

    以下基础知识转载自正点原子PDF资料. 前面例子 中创建的任务大部份时间都处于阻塞态.这种状态下所有的任务都不可运行,所以也不能被调度器选中.但处理器总是需要代码来执行——所以至少要有一个任务处于运行 ...

  5. Roslyn介绍

    介绍 一般来说,编译器是一个黑箱,源代码从一端进入,然后箱子中发生一些奇妙的变化,最后从另一端出来目标文件或程序集.编译器施展它们的魔法,它们必须对所处理的代码进行深入的理解,不过相关知识不是每个人都 ...

  6. Ubuntu 11.04 下安装配置 JDK 7

    第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...

  7. rp2836 网卡以及串口与接插件位置关系

    P3     ETH1 P6     ETH0 P7     /dev/ttyS3 调试口 P13-1  /dev/ttyS2  rs485+ P13-2  /dev/ttyS2  rs485- P1 ...

  8. BLUETOOTH:HCI层编程

    1. HCI层协议概述: Host Controller Interface(HCI)  就是用来沟通Host和Module.Host通常就是PC,Module则是以各种物理连接形式(USB,seri ...

  9. 基于Ambari构建自己的大数据平台产品

    目前市场上常见的企业级大数据平台型的产品主流的有两个,一个是Cloudera公司推出的CDH,一个是Hortonworks公司推出的一套HDP,其中HDP是以开源的Ambari作为一个管理监控工具,C ...

  10. 虚拟化–操作系统级 LXC Linux Containers内核轻量级虚拟化技术

    友情提示:非原文链接可能会影响您的阅读体验,欢迎查看原文.(http://blog.geekcome.com) 原文地址:http://blog.geekcome.com/archives/288 软 ...