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 ...
随机推荐
- contentWindow 和contentDocument区别 及iframe访问
a>contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. b>contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 ...
- gulp安装和使用简介
一. gulp和grunt对比 grunt目前的工作流程:读文件.修改文件.写文件——读文件.修改文件.写文件——... gulp目前的工作流程:读取文件——修改文件——修改文件...——写文件 二. ...
- 【汉字乱码】IE下GET形式传递汉字。
js:encodeURI(); php:urlencode(); 举例: 本来打算这样使用 <a href="list.php?plate=辖区动态" charset=&qu ...
- jquery提示信息 tips
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 如何在使用了updatepanel后弹出提示信息
转载:http://www.cnblogs.com/brusehht/archive/2009/03/19/1416802.html 常情况下,我们在使用ajax利用updatepanel实现页面局部 ...
- AJAX在Post中文的时候乱码的解决方法
加上设置字符编码的方法: response.setHeader("charset","gb2312"); *************************** ...
- Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集
C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题
B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- 从头学Android系列
从头学Android系列 http://blog.csdn.net/worker90/article/category/888358
- 关于pushState
window.pushState({}, "title", "/index.html");---------------->改变URL的值,但是并不刷新 ...