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. 大数据学习之Scala中main函数的分析以及基本规则(2)

    一.main函数的分析 首先来看我们在上一节最后看到的这个程序,我们先来简单的分析一下.有助于后面的学习 object HelloScala { def main(args: Array[String ...

  2. Linux:命令执行控制&&与||

    1.&& 方式:command1 && command2 如果command1执行成功,则执行command2 2.|| 方式:command1 || command2 ...

  3. shell脚本 批量转换目录下文件编码

    发布:JB01   来源:脚本学堂     [大 中 小] 分享一例shell脚本,实现可以批量转换目录下的文件编码,很实用的一个小shell,有需要的朋友参考下.原文地址:http://www.jb ...

  4. C++和C#实现剪切板数据交互

    c#端由于system.windows.form自带的剪切板功能太少,所以写了一个Helper类把接口转了出来.这样就可以用不同的uint的id了. 并且自带的剪切板必须执行在[STAThread]模 ...

  5. Atitit.故障排除系列---php 程序网站数据库错误排除流程

    Atitit.故障排除系列---php 程序网站数据库错误排除流程 Php页面报告的错误不能定位到myusql的db配置上...字说是db conn err Mysql 接入错误...大概查看哈能不能 ...

  6. redis基础之安装和配置(一)

    前言 折腾一下redis在linux环境的安装. ubantu16.04环境下安装 下载安装,依次执行命令; # 从官方网站下载安装包,注意,当前在哪个目录下执行命令,下载的包将在哪个目录下 $ wg ...

  7. 线程相关函数(3)-pthread_detach()将某个线程设成分离态

    #include <pthread.h>int pthread_detach(pthread_t tid); pthread_t tid: 分离线程的tid返回值:成功返回0,失败返回错误 ...

  8. python3+spark2.1+kafka0.8+sparkStreaming

    python代码: import time from pyspark import SparkContext from pyspark.streaming import StreamingContex ...

  9. python判断一个对象是否可迭代

    如何判断一个对象是可迭代对象? 方法是通过collections模块的Iterable类型判断: >>> from collections import Iterable >& ...

  10. hdu1003 最大子串和

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...