博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【POJ-1390】Blocks 区间DP
阅读量:5264 次
发布时间:2019-06-14

本文共 3235 字,大约阅读时间需要 10 分钟。

Blocks

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

Description

Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold. 
The corresponding picture will be as shown below: 
 
Figure 1
If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a 'box segment'. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively. 
Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will get k*k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4=16 points. 
Now let's look at the picture below: 
 
Figure 2
The first one is OPTIMAL. 
Find the highest score you can get, given an initial state of this game. 

Input

The first line contains the number of tests t(1<=t<=15). Each case contains two lines. The first line contains an integer n(1<=n<=200), the number of boxes. The second line contains n integers, representing the colors of each box. The integers are in the range 1~n.

Output

For each test case, print the case number and the highest possible score.

Sample Input

291 2 2 2 2 3 3 3 111

Sample Output

Case 1: 29Case 2: 1

Source

Solution

lrj出的题好劲啊......写的TA爷的做法

一个比较厉害的状态$f[l][r][k]$表示当$a[l]==a[r]$时,将$l~r$这个整段玩到还剩连续的$k$个$a[l]/a[r]$色的块时得到的最大的值。

但是单靠这个是不足以转移的,

另一个状态$g[l][r]$表示,不管以什么方法,删光$l~r$这段的得到的最大的值。

然后进行区间DP,显然要枚举区间,枚举断点,转移就是;

$$f[l][r][k]=max(f[l][r][k],f[l][r][k-1]+g[k'-1][r-1]);$$

这个转移就是把区间$l~r$中花式删掉$k'~r-1$这段,剩下的组成$k$个的最大方案。

$$g[l][r]=max(g[l][r],f[l][r][k]+k*k,g[l][k']+g[k'+1][r]);$$

这个转移比较显然..

然后这样转移之后,显然$f[l][r][0]=g[l][r]$.

答案就是$f[1][N][0]/g[1][N]$,时间复杂度是$<=O(N^4)$的,而且在正常的数据下表现非常优秀。

Code

#include
#include
#include
#include
#include
using namespace std;inline int read(){ int x=0,f=1; char ch=getchar(); while (ch<'0' || ch>'9') {
if (ch=='-') f=-1; ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f;}int T,N,a[210],f[210][210][210],g[210][210],t,cnt[210][210];int main(){ T=read(); while (T--) { N=read(); for (int i=1; i<=N; i++) a[i]=read(); memset(cnt,0,sizeof(cnt)); for (int i=1; i<=N; i++) for (int j=i+1; j<=N; j++) if (a[i]==a[j]) for (int k=i; k<=j; k++) if (a[k]==a[i]) cnt[i][j]++; memset(f,128,sizeof(f)); memset(g,0,sizeof(g)); for (int i=1; i<=N; i++) g[i][i]=f[i][i][0]=1,f[i][i][1]=0; for (int len=1; len<=N; len++) for (int l=1; l+len-1<=N; l++) { int r=l+len-1; for (int i=1; i<=cnt[l][r]; i++) { for (int j=l; j

 

转载于:https://www.cnblogs.com/DaD3zZ-Beyonder/p/6005384.html

你可能感兴趣的文章
URAL 1002 Phone Numbers(KMP+最短路orDP)
查看>>
web_day4_css_宽度
查看>>
electron入门心得
查看>>
格而知之2:UIView的autoresizingMask属性探究
查看>>
我的Hook学习笔记
查看>>
js中的try/catch
查看>>
寄Android开发Gradle你需要知道的知识
查看>>
简述spring中常有的几种advice?
查看>>
整理推荐的CSS属性书写顺序
查看>>
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>
Octotree Chrome安装与使用方法
查看>>
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
查看>>
趣谈Java变量的可见性问题
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
ssm框架之将数据库的数据导入导出为excel文件
查看>>
语音识别中的MFCC的提取原理和MATLAB实现
查看>>
验证组件FluentValidation的使用示例
查看>>