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可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- Maven deploy部署jar包到远程私仓
Maven deploy部署jar包到远程私仓 maven deploy介绍 maven中的仓库分为两种,snapshot快照仓库和release发布仓库.snapshot快照仓库用于保存开发过程中的 ...
- DataPipeline | 享物说产品负责人夏凯:数据驱动的用户增长实战
夏凯 卡内基梅隆大学计算机系毕业,曾供职于Evernote数据团队和微软Bing.com搜索引擎广告部门.回国后作为早期成员加入小红书,先后从事大数据,用户增长,项目和团队管理等工作. 我最初是在美国 ...
- 城市经纬度 json
[ { "name": "北京市", "log": "116.46", "lat": "3 ...
- 一个特殊的SQL Server阻塞案例分析
上周,在SQL Server数据库下面遇到了一个有意思的SQL阻塞(SQL Blocking)案例.其实个人对SQL Server的阻塞还是颇有研究的.写过好几篇相关文章. 至于这里为什么要总结一下这 ...
- Neuroph开发过程
文章提纲 安装与配置 开发小结 建立项目 配置项目 理解感知机的代码 安装与配置 JDK的安装:建议JRE 1.8以上: Neuroph安装:建议2.94的版本.下载地址 neuroph-core-2 ...
- 宝塔服务器面板 部署 thinkphp5 坑
thinkphp5 在宝塔服务器上部署,出现的问题: 1. File not found. 原因: 宝塔服务器默认不支持 pathinfo 的路径访问:需要在软件 - PHP - 管理里面,安装php ...
- Pandas 数据清洗常用篇
一.缺失值 sklearn中的preprocessing下有imputer,可进官方文档参考.这里主讲pandas. 拿到数据,一般先检查是否有缺失值,用isnul()或notnull(). 再决定d ...
- CSS问题
当标签之间有缝隙 两个a标签之间消除缝隙 可在div设置 font-size:0 ul下的li去掉小圆点:设置 ul list-style:none <div> <a> & ...
- Vue.Draggable 文档总结
本文章转自https://blog.csdn.net/zjiang1994/article/details/79809687 Vue.Draggable学习总结 Draggable为基于Sortabl ...
- python基础 常见用法
1.python计时器timeit模块 1)timeit 模块定义了接收两个参数的Timer类,两个参数都是字符串. 参数1:要计时的语句或者函数 参数2:为参数1构建环境的导入语句 2)Timer对 ...