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可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- 基于python的种子搜索网站,你懂得!
该项目是基于python的web类库django开发的一套web网站,给师弟做的毕业设计.本人的研究方向是一项关于搜索的研究项目.在该项目中,笔者开发了一个简单版的搜索网站,实现了对数据库数据的检索和 ...
- 工具资源系列之给虚拟机装个centos
前文我们已经讲解了如何在 mac 系统上安装虚拟机软件,这节我们接着讲解如何利用虚拟机安装 centos 镜像. 安装镜像的大致步骤基本相同,只不过是配置项略显不同而已,如果需要安装其他系统镜像,请参 ...
- 好代码是管出来的——C#的代码规范
代码是软件开发过程的产物,代码的作用是通过编译器编译后运行,达到预期的效果(功能.稳定性.安全性等等),而另外一个重要作用是给人阅读.对于机器来说只要代码正确就能够正确的运行程序,但是人不同,如果代码 ...
- Vue组件之全局组件与局部组件
1全局注册实例 <div id="app"> <com-btn></com-btn> <com-btn></com-btn&g ...
- 03 前端篇(JS)
参考博客:http://www.cnblogs.com/yuanchenqi/articles/5980312.html JavaScript包括三部分: ECMAScript.DOM.BOM Jav ...
- [认证授权] 2.OAuth2授权(续) & JWT(JSON Web Token)
1 RFC6749还有哪些可以完善的? 1.1 撤销Token 在上篇[认证授权] 1.OAuth2授权中介绍到了OAuth2可以帮我们解决第三方Client访问受保护资源的问题,但是只提供了如何获得 ...
- soamanager发布的Webservice服务,调用时出现http500报错
最近再给薪酬那边发布ws服务时出现了报错,调用方反馈了errorCode:BEA-380002.在使用XMLspy工具去调用这个WSDL时候,则反馈http500的错误消息.如下图: 遇到这种问题我通 ...
- Quick Sort(三向切分的快速排序)(Java)
//三向切分的快速排序 //这种切分方法对于数组中有大量重复元素的情况有比较大的性能提升 public static void main(String[] args) { Scanner input ...
- JS 输入框输入数字检查
<input id='ApplyInputNum' type='text' class='mytext form-control' align='left' onblur='InputCheck ...
- Asp.Net Core中DI的知识总结
在asp.net core中DI的概念是由这几部分组成的: IServiceCollection,保存IServiceDescriptor实例的列表 IServiceProvider,只有一个方法Ge ...