啊~~

被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. umijs开发实践-不同页面交叉使用dva中的modal文件导致的错误

    最近在使用umijs进行H5开发工作,在开发的过程中踩了一些坑,在这里记录一下. 1:按需加载在现在是很常见的优化方式了,我在.umirc.js中开启dynamicImport后,运行umi buil ...

  2. C#基础委托回顾

    C#基础委托回顾 前言 快忘记了. 委托的特点 委托类似于 C++ 函数指针,但它们是类型安全的. 委托允许将方法作为参数进行传递. 委托可用于定义回调方法. 委托可以链接在一起:例如,可以对一个事件 ...

  3. vue(6)—— vue中向后端异步请求

    异步请求 其实什么是异步请求已经不用多说了,通俗的说,就是整个页面不会刷新,需要更新的部分数据做局部刷新,其他数据不变. 学到这里,你应该用过jquery里的ajax了,所以很能理解了,不多说了.详细 ...

  4. Python 进程(一)理论部分

    进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行,即正在运行的程序,是系统进行资源分配和调度的基本单位,进程是对正在运行程序的一个抽象,在早期面向进程设计的计算机结构中,进程是程 ...

  5. androidkiller连接模拟器并修改源码调试

    首先需要连接模拟器,首先在模拟器的bin目录下运行命令:nox_adb.exe connect 127.0.0.1:62001(可以disconnect关闭): 之后在androidkiller的bi ...

  6. RubyGems系列之RubyGems初识

    转载请标明来源:https://www.cnblogs.com/zhanggui/p/9719291.html 一. 基础理解 RubyGems简称gems,它是一个用于对Ruby组件进行打包的Rub ...

  7. df、du命令

     EXT3  最多只能支持32TB的文件系统和2TB的文件,实际只能容纳2TB的文件系统和16GB的文件 Ext3目前只支持32000个子目录 Ext3文件系统使用32位空间记录块数量和i-节点数量 ...

  8. Core官方DI解析(5)-ServiceProviderEngine

    最后来看看前面一直说的Engine(工作引擎),工作引擎接口是IServiceProviderEngine在ServiceProvider的构造函数中看到了根据指定的Mode创建了不同的实现类,下面先 ...

  9. 64位ubuntu安装交叉编译工具链,显示找不到命令

    是因为Ubuntu64位版本已不支持ia32-libs的软件包,而是使用了lib32ncurses5.lib32z1软件包做为替代, 所以在Ubuntu16.04版本当中应该安装执行: sudo ap ...

  10. C#如何使SQLite程序集既能适应32位系统也能适应64位系统

    分享5: 需求:都知道Sqlite3是分32位和64位版本的,那如果将一个Sqlite3.dll文件全适用 分析:Sqlite是种轻量级的数据库文件,使用了混合编程而成的,一部分采用非托管的C++代码 ...