POJ1179Polygon(区间dp)

啊~~

被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:
1 f2[i][j]=max(f2[i][j],max(f1[i][k]*f1[k+1][j],f2[i][k]*f2[k+1][j]));
2 f1[i][j]=min(f1[i][j],min(f1[i][k]*f2[k+1][j],min(f2[i][k]*f1[k+1][j],f1[i][k]*f1[k+1][j])));//f1表示最大值,f2表示最小值

最关键的部分我们已经知道了,现在我们就来解决答案输出的问题,

其实我们要得到题目最大值同种最大值的多种情况

我们先来解决一下最大值的问题:

1 for(int i=1;i<=n;i++){
2         ans=max(ans,f2[i][i+n-1]);//表示在所有的点缩完后的答案
3     }

这样我们就得到了最大值。

然后我们来处理多情况问题。即我们去判断这个断边情况的答案,如果相等则输出。(因为是顺序的扫描,所以输出一定是有序的)。

1 for(int i=1;i<=n;i++){
2         if(f2[i][i+n-1]==ans){
3             printf("%d ",i);
4         }
5     }
这样我们就结束了这道题(注意毒瘤读入):

 1 //#include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<string>
 5 #include<cmath>
 6 using namespace std;
 7 template<typename type_of_scan>
 8 void scan(type_of_scan &x){
 9     type_of_scan f=1;x=0;char s=getchar();
10     while(s<‘0‘||s>‘9‘){if(s==‘-‘)f=-1;s=getchar();}
11     while(s>=‘0‘&&s<=‘9‘){x=x*10+s-‘0‘;s=getchar();}
12     x*=f;
13 }
14 const int N=1007;
15 int n;
16 int f1[N][N],f2[N][N],a[N];
17 char c[N];
18 int main(){
19      scan(n);
20 //    scanf("%d\n",&n);
21      for(int i=1;i<=n;i++){
22 //         scanf("%c %d",&c[i],&a[i]);getchar();//ioi读入 ,虽然我也不知道为什么
23          scanf("%c",&c[i]);
24          c[i+n]=c[i];
25          scan(a[i]);
26          a[i+n]=a[i];
27      }//拆成两倍的链
28      for(int i=1;i<=n*2;i++){
29          for(int j=1;j<=n*2;j++){
30              f1[i][j]=0x3f3f3f3f;//最小值
31              f2[i][j]=-0x3f3f3f3f;//最大值
32              if(i==j)f1[i][j]=f2[i][j]=a[i];//赋值点
33          }
34      }
35     for(int l=2;l<=n;l++){
36         for(int i=1;i<=n*2-l+1;i++){
37             int j=i+l-1;
38             for(int k=i;k<j;k++){
39                 if(c[k+1]==‘x‘){
40                     f2[i][j]=max(f2[i][j],max(f1[i][k]*f1[k+1][j],f2[i][k]*f2[k+1][j]));
41                     f1[i][j]=min(f1[i][j],min(f1[i][k]*f2[k+1][j],min(f2[i][k]*f1[k+1][j],f1[i][k]*f1[k+1][j])));
42                 }else{
43                     f2[i][j]=max(f2[i][j],f2[i][k]+f2[k+1][j]);
44                     f1[i][j]=min(f1[i][j],f1[i][k]+f1[k+1][j]);
45                 }
46             }
47         }
48     }
49     int ans=0;
50     for(int i=1;i<=n;i++){
51         ans=max(ans,f2[i][i+n-1]);//表示在所有的点缩完后的答案
52     }
53     printf("%d\n",ans);
54     for(int i=1;i<=n;i++){
55         if(f2[i][i+n-1]==ans){
56             printf("%d ",i);
57         }
58     }
59 return 0;
60 }

AC代码

结束。



原文地址:https://www.cnblogs.com/xishirujin/p/10691830.html

时间: 2024-08-01 15:03:57

POJ1179Polygon(区间dp)的相关文章

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi

算法复习——区间dp

感觉对区间dp也不好说些什么直接照搬讲义了2333 例题: 1.引水入城(洛谷1514) 这道题先开始看不出来到底和区间dp有什么卵关系···· 首先肯定是bfs暴力判一判可以覆盖到哪些城市····无解直接输出···有解得话就要想想了···· 这道题关键是要发现··如果一个蓄水池所在城市可以覆盖到一些沙漠城市···那么这些沙漠城市肯定是一段····不然假设有一个城市是断开的而两边都被同一个蓄水池流出的水覆盖,这个城市四周的城市都肯定比它矮···(不理解举个反例吧···反正我举不出来)···然后就

合并石子 区间dp水题

合并石子 链接: nyoj 737 描述: 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值. tags:最基本的区间dp,这题范围小,如果n大一些,还是要加个平行四边行优化. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring&g

Luogu P2734 游戏 A Game 区间DP

P2734 游戏 A Game 题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该数字被去掉并累加到本玩家的得分中,当数取尽时,游戏结束.以最终得分多者为胜. 题目描述 编一个执行最优策略的程序,最优策略就是使玩家在与最好的对手对弈时,能得到的在当前情况下最大的可能的总分的策略.你的程序要始终为第二位玩家执行最优策略. 输入输出格式 输入格式: 第一行: 正整数N, 表示序列中正整数的个数

HDU-4283 You Are the One (区间DP)

Problem Description The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there ar

lightoj1031_区间dp

题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 题目描述: 给出一个数列,两人轮流取数, 取完结束.每次可以取好多个数,但是只能从首或者尾为起点取连续的若干个.问最后两者取数和的绝对值最大为多少? 区间dp: 这道题我是在看了几份阶梯报告之后才想通的,现在想想很符合动态规划的要求 d(i, j)表示取数的人在数组i 到 j中能取的的最大值,然后中间枚举分割点, ans = max(ans, sum[k]-sum[i-1]-d

lightoj1025_区间dp

题目链接:http://lightoj.com/volume_showproblem.php?problem=1025 题目描述: 给出一个字符串,可以任意删除位置的字符,也可以删除任意多个.问能组成多少个回文串? 解题思路: 自从开始学dp,感觉自己智商一直处于离线状态.席八啊啊啊啊啊啊!今天随机到这个题目,看了好久竟然没有看出来是区间DP.知道是区间DP后立马感觉明白. 情景设定 dp[l][r] 表示 区间 [l, r] 内的回文串数目. 状态转移:dp[l][r] = dp[l][r-1

POJ 2955 Brackets (区间DP)

题意:给定一个序列,问你最多有多少个合法的括号. 析:区间DP,dp[i][j] 表示在 第 i 到 第 j 区间内最多有多少个合法的括号. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <ios