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 ;
}

最新文章

  1. JS根据经纬度获取地址信息
  2. python学习 3笔记
  3. Excellent Articles
  4. Linux内核循环链表经典分析和移植
  5. 支撑5亿用户、1.5亿活跃用户的Twitter最新架构详解及相关实现
  6. ListBox mvvm 学习笔记
  7. Unity3D-Baked Lightmapping 示例学习
  8. [Angular 2] Using events and refs
  9. java设计模式之六适配器模式(Adapter)
  10. sublime工具篇
  11. Interface request structure used for socket ioctl&#39;s
  12. 01-python中字符串的常见操作
  13. 大数据技术生态圈形象比喻(Hadoop、Hive、Spark 关系)
  14. springmvc中只接受固定提交内容类型的请求
  15. VS编程,C#串口通讯,通过串口读取数据的一种方法
  16. Ubuntu 16.04安装下HTK--亲测ok
  17. [BZOJ 2705] [SDOI 2012] Longge的问题
  18. APScheduler(Python化的Cron)使用总结 定时任务
  19. 【xsy2303】呀 dp
  20. java Dom4j xml 写

热门文章

  1. 输入LPCWSTR类型字符串
  2. Atititjs javascript异常处理机制与java异常的转换.js exception process
  3. CentOS 7.0 关闭firewalld防火墙指令 及更换Iptables防火墙
  4. gdb,程序调试助手
  5. python学习笔记(10)--爬虫下载煎蛋图片
  6. EmWebAdmin 导航栏分析
  7. flume中Source
  8. eclipse代码补全按键修改成Tab
  9. 网页抓取信息(php正則表達式、php操作excel)
  10. Ubuntu Python 安装numpy SciPy、MatPlotLib环境