POJ1179Polygon(区间dp)
啊~~
被dp摁在地上摩擦的人
今天做了一道区间dp的题(POJ1179Polygon)
题目:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6951 | Accepted: 2975 |
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
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
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
题目翻译:
被某种生物吃掉了~~懒
自行翻译╮( ̄▽ ̄)╭ 好了,我们现在来看这道题,刚开始拿到这道题时,我看的十分懵逼,恕我直言,我是看了题解的= =b
首先,我们先弄懂题意,让我们首先去拿去一条边,是这个环换为一条链,所以,这个时候我们就有一步操作,来将环变为链。每一回把值赋到当前点和加n的点上。
然后,我们看到操作是将两个数合为一个,和石子合并很相像,所以我们就可以沿用石子合并的思路,区间动态规划。
这时我们就来考虑数值转移时的最大最小值,我们很容易想到用两个数组来进行维护最大最小值。而且在转移时的最大值和最小值分为+和*两种不同结构
第一,当为+时,最大值就是两阶段最大值相加,最小值是两阶段最小值相加。
其次,我们来看*的情况,不难想到,我们可以想到最大值*最大值,但是我们可以知道最大最小值有负数,所以此时最大值就不唯一,比如说最小*最小,负*负=正!!
所以,我们就有多种情况进行讨论。
code:
f2[i][j]=max(f2[i][j],max(f1[i][k]*f1[k+][j],f2[i][k]*f2[k+][j]));
f1[i][j]=min(f1[i][j],min(f1[i][k]*f2[k+][j],min(f2[i][k]*f1[k+][j],f1[i][k]*f1[k+][j])));//f1表示最大值,f2表示最小值
最关键的部分我们已经知道了,现在我们就来解决答案输出的问题,
其实我们要得到题目最大值同种最大值的多种情况
我们先来解决一下最大值的问题:
for(int i=;i<=n;i++){
ans=max(ans,f2[i][i+n-]);//表示在所有的点缩完后的答案
}
这样我们就得到了最大值。
然后我们来处理多情况问题。即我们去判断这个断边情况的答案,如果相等则输出。(因为是顺序的扫描,所以输出一定是有序的)。
for(int i=;i<=n;i++){
if(f2[i][i+n-]==ans){
printf("%d ",i);
}
}
这样我们就结束了这道题(注意毒瘤读入):
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
using namespace std;
template<typename type_of_scan>
void scan(type_of_scan &x){
type_of_scan f=;x=;char s=getchar();
while(s<''||s>''){if(s=='-')f=-;s=getchar();}
while(s>=''&&s<=''){x=x*+s-'';s=getchar();}
x*=f;
}
const int N=;
int n;
int f1[N][N],f2[N][N],a[N];
char c[N];
int main(){
scan(n);
// scanf("%d\n",&n);
for(int i=;i<=n;i++){
// scanf("%c %d",&c[i],&a[i]);getchar();//ioi读入 ,虽然我也不知道为什么
scanf("%c",&c[i]);
c[i+n]=c[i];
scan(a[i]);
a[i+n]=a[i];
}//拆成两倍的链
for(int i=;i<=n*;i++){
for(int j=;j<=n*;j++){
f1[i][j]=0x3f3f3f3f;//最小值
f2[i][j]=-0x3f3f3f3f;//最大值
if(i==j)f1[i][j]=f2[i][j]=a[i];//赋值点
}
}
for(int l=;l<=n;l++){
for(int i=;i<=n*-l+;i++){
int j=i+l-;
for(int k=i;k<j;k++){
if(c[k+]=='x'){
f2[i][j]=max(f2[i][j],max(f1[i][k]*f1[k+][j],f2[i][k]*f2[k+][j]));
f1[i][j]=min(f1[i][j],min(f1[i][k]*f2[k+][j],min(f2[i][k]*f1[k+][j],f1[i][k]*f1[k+][j])));
}else{
f2[i][j]=max(f2[i][j],f2[i][k]+f2[k+][j]);
f1[i][j]=min(f1[i][j],f1[i][k]+f1[k+][j]);
}
}
}
}
int ans=;
for(int i=;i<=n;i++){
ans=max(ans,f2[i][i+n-]);//表示在所有的点缩完后的答案
}
printf("%d\n",ans);
for(int i=;i<=n;i++){
if(f2[i][i+n-]==ans){
printf("%d ",i);
}
}
return ;
}
AC代码
结束。
POJ1179Polygon(区间dp)的更多相关文章
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
- Uva 10891 经典博弈区间DP
经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- 数据结构学习java(一点五)链式顺序表(链表)
java中没有将指针暴露给用户(以前做过看过一篇文章写有java中是有指针的,只是被藏起来了),所以得使用引用的方式. 何为引用请看下面这篇文章(写的很不错,当然肯定比我写的好): https://w ...
- SQL SERVER 排查脚本
随着数据量和并发量的增大,数据库有时会遇到CPU,内存,IO 性能问题:整理了一下有关排查数据相关的SQL脚本,以便排查问题之用: 1,哪些SQL 消耗CPU /* 查看哪些SQL语句消耗CPU,找 ...
- C# -- 使用FileInfo获取文件信息
C# -- 使用FileInfo获取文件信息 1. 代码实现 static void Main(string[] args) { GetFileInfo(@"D:\Test.xlsx&quo ...
- vim编辑器操作
vim被称为编辑器之神,另外一个是sublime.vim较vi比较高级,vi适用于文本编辑,vim更加适合于coding.凡是vim里面的命令在vi都是适用的. vim的大众版的三种模式(其实不止三种 ...
- Linux命令大杂烩
查看linux出口IP curl ifconfig.me scp跨服务器转移文件命令 scp 文件 root@IP:/application/apache-tomcat-8.0.36 回车, ...
- python接口自动化-session_自动发文
一.session简介 查看 requests.session() 帮助文档(只贴了一部分内容) import requests help(requests.session()) class Sess ...
- HTML5移动端触摸事件
一.移动端事件问题 1.click事件300ms延迟问题 2007年第一代iphone发布,移动端Safari首创双击缩放功能,原理是click一次后,经过300ms之后检测是否再有一次click,如 ...
- RMQ区间最大值与最小值查询
RMQ复杂度:建表$O\left ( nlgn \right ) $,查询$O\left ( 1 \right )$ ll F_Min[maxn][20],F_Max[maxn][20]; void ...
- PHP为前端CSS和JS增加时间戳版本号
一.PHP代码如下: function addVersion($url){ $version = date("Y-m-d H:i:s",filemtime($_SERVER['DO ...
- Neutron vxlan network--L2 Population
L2 Population 是用来提高 VXLAN 网络 Scalability 的. 通常我们说某个系统的 Scalability 好,其意思是: 当系统的规模变大时,仍然能够高效地工作. L2 ...