poj1390
来源:互联网 发布:ajax提交form表单json 编辑:程序博客网 时间:2024/05/29 21:18
Blocks
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 5713 Accepted: 2356
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.
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
AC代码:
#include<cstdio>#include<string.h>#include<cstring>#include<iostream>#include<algorithm>#include<math.h>using namespace std;int len[205],dp[205][205][205],color[205];int DP(int x,int y,int k){ if(dp[x][y][k]) return dp[x][y][k]; if(x==y) return (len[x]+k)*(len[x]+k); dp[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]) { dp[x][y][k]=max(dp[x][y][k],DP(x,i,len[y]+k)+DP(i+1,y-1,0)); } } return dp[x][y][k];}int main(){ int t; scanf("%d",&t); for(int r=1;r<=t;r++) { int n; scanf("%d",&n); int num=0; memset(len,0,sizeof(len)); memset(color,0,sizeof(color)); for(int i=1;i<=n;i++) { int temp; scanf("%d",&temp); if(color[num]==temp) len[num]++; else { num++; len[num]++; color[num]=temp; } } memset(dp,0,sizeof(dp)); int ans=DP(1,num,0); printf("Case %d: ",r); printf("%d\n",ans); } return 0;}
阅读全文
1 0
- poj1390
- poj1390
- poj1390 Blocks
- poj1390 Blocks
- poj1390 方块消除 dp
- poj1390-dp消方块
- POJ1390 Blocks 【动态规划】
- poj1390解题报告
- poj1390 2010.8.1
- poj1390 2010.8.2
- POJ1390 Blocks 区间DP
- [POJ1390]Blocks(dp)
- poj1390 Blocks(区间dp)
- poj1390 (区间dp)
- [POJ1390]Blocks(方块消除)
- [POJ1390]Blocks(区间dp)
- poj1390区间dp+记忆化搜索
- Composer 安装与使用
- java编程题
- 清除浮动详解
- 安装aix 6.1系统的完整教程_初学者都可以学会
- Java基本数据类型在内存中的存储大小实践
- poj1390
- Mac nvm安装细节备忘
- iOS_通话时间的计算
- 7月26日云栖精选夜读:MySQL金融版线上发布会:它为什么是金融企业的首选_技术大佬、产品和神秘嘉宾本位“演绎”
- UE4_代理(Delegate)
- 教你少走点弯路
- Mysql编码之查看、修改总结
- PHP 开发者该知道的 5 个 Composer 小技巧
- Spark RDD基本操作