poj1179 Polygon【区间DP】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:6633 | Accepted: 2834 |
Description
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
3 <= N <= 50
For any sequence of moves, vertex labels are in the range [-32768,32767].
Output
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 ;
}
最新文章
- JS根据经纬度获取地址信息
- python学习 3笔记
- Excellent Articles
- Linux内核循环链表经典分析和移植
- 支撑5亿用户、1.5亿活跃用户的Twitter最新架构详解及相关实现
- ListBox mvvm 学习笔记
- Unity3D-Baked Lightmapping 示例学习
- [Angular 2] Using events and refs
- java设计模式之六适配器模式(Adapter)
- sublime工具篇
- Interface request structure used for socket ioctl&#39;s
- 01-python中字符串的常见操作
- 大数据技术生态圈形象比喻(Hadoop、Hive、Spark 关系)
- springmvc中只接受固定提交内容类型的请求
- VS编程,C#串口通讯,通过串口读取数据的一种方法
- Ubuntu 16.04安装下HTK--亲测ok
- [BZOJ 2705] [SDOI 2012] Longge的问题
- APScheduler(Python化的Cron)使用总结 定时任务
- 【xsy2303】呀 dp
- java Dom4j xml 写