啊~~

被dp摁在地上摩擦的人

今天做了一道区间dp的题(POJ1179Polygon

题目:

Polygon
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6951   Accepted: 2975

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
题目翻译:
被某种生物吃掉了~~懒
自行翻译╮( ̄▽ ̄)╭ 好了,我们现在来看这道题,刚开始拿到这道题时,我看的十分懵逼,恕我直言,我是看了题解的= =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)的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. 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 ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. 在Windows Phone 8.1中使用Sqlite数据库

    前言 我的工作目前不涉及到Windows phone的开发,但是业余时间也开发过几款app.以前由于各种条件的限制,只接触到WP8.0设备的app开发. 最近几个月开始将WP8的应用迁移到WP8.1, ...

  2. 章节十、8-XPath---如何构建有效的XPath

    以下演示操作以该网址中的内容为例:https://learn.letskodeit.com/?_ga=2.143454972.85111248.1555037144-697706367.1554889 ...

  3. C#零基础入门-2-Visual Studio (VS)程序初始化及各组成部分

    X:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe 可以使用桌面快捷方式启动,也可以从开始菜单启动,还 ...

  4. windows已激活,但提示:windows 7 内部版本7601 此windows副本不是正版

    把三个服务改为自动:

  5. hbase 过滤器 rowfilter

    HBase为筛选数据提供了一组过滤器,通过这个过滤器可以在HBase中的数据的多个维度(行,列,数据版本)上进行对数据的筛选操作,也就是说过滤器最终能够筛选的数据能够细化到具体的一个存储单元格上(由行 ...

  6. 网络流 E - Escape HDU - 3605

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...

  7. Servlet开发笔记(二)

    ServletContext对象 WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用.        ServletConfig对象中维 ...

  8. Net包管理NuGet(3)搭建私服及引用私服的包

    1,打开vs创建项目(ASP.NET WEB空项目)假设命名为MyNuGet 空项目解决方案如图 2,右键引用>管理NuGet程序包>切到浏览搜索NuGet.Server然后安装(3.1. ...

  9. (六)jdk8学习心得之Stream流

    六.Stream流 1. 什么是stream流 现阶段,可以把stream流看成一个高级版的Iterator.普通的Iterator只能实现遍历,遍历做什么,就需要具体些功能代码函数了.而这个stre ...

  10. dva

    import React, { PureComponent } from "react"; import { Chart, Geom, Axis, Tooltip, Coord, ...