poj 1390 动态规划
思路:
黑书的例题
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define Maxn 210
using namespace std;
int dp[Maxn][Maxn][Maxn],col[Maxn],len[Maxn],pre[Maxn],aper[Maxn],after[Maxn],vi[Maxn];
int main()
{
int t,n,i,j,x,k,Case=;
int cnt=;
scanf("%d",&t);
while(t--){
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
memset(len,,sizeof(len));
memset(aper,,sizeof(aper));
cnt=;
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&x);
if(x!=col[cnt])
col[++cnt]=x,len[cnt]=,pre[cnt]=aper[x],aper[x]=cnt;
else
len[cnt]++;
}
memset(vi,,sizeof(vi));
for(i = cnt; i >= ; i--){
if(vi[i]) continue;
after[i] = ;
int tmp = i;
for(j = i-; j >= ; j--)
if(col[j] == col[tmp]){
after[j] = after[tmp] + len[tmp];
tmp = j;
vi[j--] = true;
}
}
int l;
for(l=;l<=cnt;l++){
for(i=;i+l<=cnt;i++){
j=i+l;
for(k=;k<=after[j];k++){
dp[i][j][k]=dp[i][j-][]+(len[j]+k)*(len[j]+k);
int p=pre[j];
while(p>=i){
dp[i][j][k]=max(dp[i][j][k], dp[i][p][k+len[j]]+dp[p+][j-][]);
p=pre[p];
}
}
}
}
printf("Case %d: %d\n",++Case,dp[][cnt][]);
}
return ;
}
poj 1390 动态规划的更多相关文章
- poj 1390 Blocks
poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...
- POJ 1390 Blocks(记忆化搜索+dp)
POJ 1390 Blocks 砌块 时限:5000 MS 内存限制:65536K 提交材料共计: 6204 接受: 2563 描述 你们中的一些人可能玩过一个叫做“积木”的游戏.一行有n个块 ...
- 【POJ 1390】Blocks
http://poj.org/problem?id=1390 黑书上的例题,感觉我这辈子是想不到这样的dp了QAQ \(f(i,j,k)\)表示将\(i\)到\(j\)合并,并且假设未来会有\(k\) ...
- nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)
17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...
- poj 3034 动态规划
思路:这是一道坑爹的动态规划,思路很容易想到,就是细节. 用dp[t][i][j],表示在第t时间,锤子停在(i,j)位置能获得的最大数量.那么只要找到一个点转移到(i,j)收益最大即可. #incl ...
- poj 2498 动态规划
思路:简单动态规划 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...
- poj 2287 动态规划
用贪心简单证明之后就是一个从两头取的动态规划 #include <iostream> #include <cstring> #include <cstdio> #i ...
- poj 1390 区间dp
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5035 Accepted: 2065 Descriptio ...
- poj 1390 Blocks (经典区间dp 方块消除)
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4250 Accepted: 1704 Descriptio ...
随机推荐
- ASP.NET验证控件RegularExpressionValidator的常见表达式
可以输入非0和0开头的数字:“^(0*[1-9][0-9]*|[1-9][0-9]*)$”只能输入数字:“^[0-9]*$”只能输入n位的数字:“^\d{n}$”只能输入至少n位数字:“^\d{n,} ...
- How to bind to data when the DataContext is not inherited【项目】
http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherit ...
- 【转】2D动画:view的Matrix
Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,太简单了,对应位相加就好.图像处理,主要用到的是乘法 ...
- OC:继承、初始化方法、便利构造器
A继承 的作用就是为了,节省代码. 继承 :子类会继承父类 里面所有的内容 思想 :凡是继承过来的实例变量和方法 都是自己的(是从父类遗传过来的) 继承的使用场景 : 当我们多 ...
- Using Eclipse With CloudStack
As part of switching to Apache Maven for building CloudStack, the .classpath and .project files used ...
- CXF 与Spring整合配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...
- 【转】 解决IllegalStateException: Can not perform this action after onSaveInstanceState
今天使用Fragment的时候,出现了这个错误 IllegalStateException: Can not perform this action after onSaveInstanceState ...
- hdu 5264 pog loves szh I 水题
pog loves szh I Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- zookeeper集群安装配置
http://www.firefoxbug.net/?p=2565
- HTML5 API——无刷新更新地址 history.pushState/replaceState 方法
尽 管是上面讲到的<JavaScript高级程序设计>(第二版)中提到,BOM中的location.path/query…… (window.location)在通过JavaScript更 ...