POJ 1390 Blocks
2018-03-29 15:14
274 查看
Blocks
DescriptionSome 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.
InputThe 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.OutputFor each test case, print the case number and the highest possible score.Sample Input2
9
1 2 2 2 2 3 3 3 1
1
1Sample OutputCase 1: 29
Case 2: 1首先将颜色相同的色块合并成大块,用num[i]来表示第i个大块中有多少相同颜色的小块,用c[i]来表示大块的颜色。第一反应想的一般可能是dp[i][j]来表示区间[i,j]的最大积分,那么对于最右边的连续色块有两种操作。第一种就是直接消掉,那么最高分就是dp[i][j-1]+(num[j]*num[j]),第二种就是在j大块之前可能有和j颜色相同的大块k,那么与其合并再消除,最高分为dp[i][k-1]+dp[k+1][j-1]+(num[k]+num[j])*(num[k]+num[j])。
可是这样考虑不全面,因为j与k合并之后不一定是最佳方案,可能在k前还有相同颜色的色块,再和之前的合并才能得到最高分。所以还需要再开一维来表示最右边色块的状态,也就是dp[i][j][len],表示在j的右边还有长度为len颜色和j一样的色块存在。那么状态转移方程就是dp[i][j][len]=max{dp[i][j-1][0]+(num[j]+len)*(num[j]+len),dp[i][k][len+num[j]]+dp[k+1][j-1][0],(i<=k<=j-2)&&c[k]==c[j]}#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=210;
int dp
;
int color
,num
,c
;
int f(int i,int j,int len)
{
if(i==j)
return dp[i][j][len]=(num[j]+len)*(num[j]+len);
if(dp[i][j][len])
return dp[i][j][len];
int temp=f(i,j-1,0)+(len+num[j])*(len+num[j]);
for(int k=i;k<j-1;k++)
if(c[k]==c[j])
temp=max(temp,f(i,k,num[j]+len)+f(k+1,j-1,0));
return dp[i][j][len]=temp;
}
int main()
{
int t,n,kase=0;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&color[i]);
int m=0;
for(int i=1;i<=n;i++)
{
if(color[i]==color[i-1])
num[m]++;
else
{
m++;
c[m]=color[i];
num[m]=1;
}
}
printf("Case %d: %d\n",++kase,f(1,m,0));
}
return 0;
}
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5966 | Accepted: 2456 |
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.
InputThe 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.OutputFor each test case, print the case number and the highest possible score.Sample Input2
9
1 2 2 2 2 3 3 3 1
1
1Sample OutputCase 1: 29
Case 2: 1首先将颜色相同的色块合并成大块,用num[i]来表示第i个大块中有多少相同颜色的小块,用c[i]来表示大块的颜色。第一反应想的一般可能是dp[i][j]来表示区间[i,j]的最大积分,那么对于最右边的连续色块有两种操作。第一种就是直接消掉,那么最高分就是dp[i][j-1]+(num[j]*num[j]),第二种就是在j大块之前可能有和j颜色相同的大块k,那么与其合并再消除,最高分为dp[i][k-1]+dp[k+1][j-1]+(num[k]+num[j])*(num[k]+num[j])。
可是这样考虑不全面,因为j与k合并之后不一定是最佳方案,可能在k前还有相同颜色的色块,再和之前的合并才能得到最高分。所以还需要再开一维来表示最右边色块的状态,也就是dp[i][j][len],表示在j的右边还有长度为len颜色和j一样的色块存在。那么状态转移方程就是dp[i][j][len]=max{dp[i][j-1][0]+(num[j]+len)*(num[j]+len),dp[i][k][len+num[j]]+dp[k+1][j-1][0],(i<=k<=j-2)&&c[k]==c[j]}#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=210;
int dp
;
int color
,num
,c
;
int f(int i,int j,int len)
{
if(i==j)
return dp[i][j][len]=(num[j]+len)*(num[j]+len);
if(dp[i][j][len])
return dp[i][j][len];
int temp=f(i,j-1,0)+(len+num[j])*(len+num[j]);
for(int k=i;k<j-1;k++)
if(c[k]==c[j])
temp=max(temp,f(i,k,num[j]+len)+f(k+1,j-1,0));
return dp[i][j][len]=temp;
}
int main()
{
int t,n,kase=0;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&color[i]);
int m=0;
for(int i=1;i<=n;i++)
{
if(color[i]==color[i-1])
num[m]++;
else
{
m++;
c[m]=color[i];
num[m]=1;
}
}
printf("Case %d: %d\n",++kase,f(1,m,0));
}
return 0;
}
相关文章推荐
- POJ - 1390 Blocks
- poj 1390 Blocks (记忆化搜索)
- poj 1390 Blocks (区间dp)
- POJ 1390 Blocks (区间DP)
- poj 1390 Blocks(dp)
- POJ 1390 Blocks (DP)
- [POJ1390]Blocks(dp)
- poj1390 Blocks
- poj 1390(消除方块(blocks))
- [poj1390]Blocks(方块消除)
- POJ 1390 Blocks
- Blocks - POJ 1390 dp
- POJ1390 Blocks 【动态规划】
- POJ1390 Blocks——动态规划——pku1390
- poj 1390 Blocks
- poj(1390)——Blocks
- poj 1390 Blocks(dp,黑书dp方块消除)
- poj 1390 Blocks (记忆化搜索)
- POJ 1390 Blocks 笔记
- poj 1390 Blocks