题目:https://www.luogu.org/problemnew/show/UVA10559

应该想到区间dp。但怎么设计状态?

因为连续的东西有分值,所以应该记录一下连续的有多少个。

  只要记录与边界连续的有多少个就能涵盖所有的连续了。只记一边的边界即可。

两个转移:用掉记录的那些连续的 或 在自己区间中再找一个一样颜色的使连续数量+1。

  用掉了以后剩余部分的连续长度就是0。也许 j-1 和 j 同色,但这个可以在另一个转移里体现,所以没问题。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int T,n,a[N],dp[N][N][N];
int rdn()
{
int ret=,fx=; char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=-; ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return ret*fx;
}
void init()
{
memset(dp,,sizeof dp);
}
int main()
{
T=rdn();
for(int t=;t<=T;t++)
{
init();
n=rdn(); for(int i=;i<=n;i++) a[i]=rdn();
for(int i=;i<=n;i++)
for(int k=;k<=(n-i);k++)
dp[i][i][k]=(k+)*(k+);
for(int d=,lm=n-d+;d<=n;d++)
for(int i=,j,LM;i<=lm;i++)
{
j=i+d-; LM=n-j;
for(int k=;k<=LM;k++)
{
dp[i][j][k]=dp[i][j-][]+(k+)*(k+);
for(int l=i;l<j;l++)
if(a[l]==a[j])
dp[i][j][k]=max(dp[i][j][k],
dp[i][l][k+]+dp[l+][j-][]);
//printf("dp[%d][%d][%d]=%d\n",i,j,k,dp[i][j][k]);
}
}
printf("Case %d: %d\n",t,dp[][n][]);
}
return ;
}

最新文章

  1. 关于外部引用JS,中文乱码的问题
  2. hibernate映射文件one-to-one
  3. qsort函数用法
  4. C# - 集合类 - 集合接口
  5. Using ROWNUM in Oracle
  6. hdu 2795 段树--点更新
  7. 放弃FreeMark?
  8. python---scrapy之MySQL同步存储
  9. 一周中的后两天 笔记(网路基础 Python基础)
  10. Java设计模式(五)Prototype原型模式
  11. 再读vue2.0
  12. eclispe集成Scalas环境后,导入外部Spark包报错:object apache is not a member of package org
  13. Spring AOP 入门实例详解
  14. git一些实践命令,关于reabse和merge的区别在最后做了一个比较
  15. LeetCode-714.Best Time to Buy and Sell Stock with Transaction Fee
  16. 前端 HTML 常用标签 head标签相关内容 style标签 定义内部样式表
  17. windows10如何查看wifi密码
  18. java_12多态
  19. Linux下利用json-c从一个json数组中提取每一个元素中的部分字段组成一个新json数组
  20. Window10激活

热门文章

  1. 为什么阿里巴巴不建议在for循环中使用&quot;+&quot;进行字符串拼接
  2. 用GetTickCount()计算一段代码执行耗费的时间的小例子
  3. 多媒体开发之---h264 图像参数级语义
  4. 3_Jsp标签_简单标签_防盗链和转义标签的实现
  5. 2_Jsp标签_传统标签功能简介
  6. Android-解决Fail to post notification on channel &quot;null&quot;的方法
  7. Project Euler:Problem 41 Pandigital prime
  8. 字符数组和strcpy
  9. GS与网络打交道
  10. php中$t=date()函数参数意义及时间更改