[POJ1390]Blocks(方块消除)
来源:互联网 发布:淘宝介入是什么意思 编辑:程序博客网 时间:2024/06/04 18:51
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
2
9
1 2 2 2 2 3 3 3 1
1
1
Sample Output
Case 1: 29
Case 2: 1
题目大意
给定n个不同颜色的盒子,连续的相同颜色的k个盒子可以拿走,权值为k*k,求把所有盒子拿完的最大权值。
题解
合并初始相邻相同的块,得到颜色数组c和对应的长度len.
例如 1 1 1 1 1 3 2 2 1 1 1 可记为
状态转移方程两种:
1.单独消除,
2.和区间段
代码
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>const int size = 200+10;int len[size],f[size][size][size],n;int color[size],flag;int T,ans,tmp;inline int dp(int x,int y,int k) { if(f[x][y][k]) return f[x][y][k]; if(x==y) return (len[x]+k)*(len[x]+k); f[x][y][k]=dp(x,y-1,0)+(len[y]+k)*(len[y]+k); for(int i=x;i<y;i++) { if(color[y]==color[i]) f[x][y][k]=std::max(f[x][y][k],dp(x,i,len[y]+k)+dp(i+1,y-1,0)); } return f[x][y][k];}int main() { scanf("%d",&T); for(int t=1;t<=T;t++) { scanf("%d",&n);flag=0; memset(len,0,sizeof len); memset(color,0,sizeof color); for(int i=0;i<n;i++) { scanf("%d",&tmp); if(color[flag]==tmp) len[flag]++; else { flag++;len[flag]++;color[flag]=tmp; } } memset(f,0,sizeof f); ans=dp(1,flag,0); printf("Case %d: ",t);printf("%d\n",ans); } return 0;}
- [POJ1390]Blocks(方块消除)
- poj1390 方块消除 dp
- 10559 - Blocks(方块消除|DP)
- [POJ1390]Blocks(dp)
- poj 1390 Blocks 方块消除
- [POJ1390]Blocks(区间dp)
- poj-1390-Blocks(方块消除)-DP
- poj 1390 Blocks (经典区间dp 方块消除)
- poj1390 Blocks
- poj1390 Blocks
- poj1390-dp消方块
- POJ1390 Blocks 【动态规划】
- POJ1390 Blocks 区间DP
- poj1390 Blocks(区间dp)
- poj 1390 Blocks(dp,黑书dp方块消除)
- 方块消除 (黑书)
- 消除pdf乱码方块
- POJ 1390 方块消除
- xcode8.2.1支持iOS8.3.1系统 添加设备依赖库文件
- hibernate学习之核心api
- neo4j 的使用说明(三)
- PhpStorm 配置在浏览器中打开PHP文件
- Mybatis源码学习笔记(二) 配置介绍
- [POJ1390]Blocks(方块消除)
- 把内存中的数据转化成16进制显示
- Web Storage 本地存储sessionStorage
- OpenCV学习笔记(20)关于opencv新版本中rows和cols的理解
- 简单进度条插件JS
- linux du命令用法
- 对称的二叉树(Java实现)
- Spring之二,基础深入,理解动态代理
- 以两军问题为背景来演绎Basic Paxos