poj 1390 区间dp
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 5035 | Accepted: 2065 |
Description
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
Output
Sample Input
2
9
1 2 2 2 2 3 3 3 1
1
1
Sample Output
Case 1: 29
Case 2: 1
Source
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
using namespace std;
int t;
int n;
struct node
{
int color;
int len;
}se[];
int score[][][];
int fun(int l,int r,int len)
{
if(score[l][r][len]>)
return score[l][r][len];
int re=(se[r].len+len);//直接处理右侧
re=re*re;
if(l==r)
{
score[l][r][len]=re;
return score[l][r][len];
}
re+=fun(l,r-,);
for(int j=r-;j>=l;j--)//枚举左侧片段
{
if(se[j].color!=se[r].color) continue;
int temp=fun(l,j,se[r].len+len)+fun(j+,r-,);//递归 分解 将右侧的合并到左侧
if(temp<=re) continue;//判断那个更优
re=temp;
break;
}
score[l][r][len]=re;
return score[l][r][len];
}
int main()
{
scanf("%d",&t);
int coun=;
int flag=;
memset(se,,sizeof(se));
while(t--){
int coun=;
int exm;
scanf("%d",&n);
scanf("%d",&se[coun].color);
se[coun].len=;
for(int i=;i<n;i++)//分解片段的过程
{
scanf("%d",&exm);
if(exm==se[coun].color)
se[coun].len++;
else
{
coun++;
se[coun].color=exm;
se[coun].len=;
}
}
memset(score,,sizeof(score));
printf("Case %d: %d\n",flag++,fun(,coun,));
}
return ;
}
poj 1390 区间dp的更多相关文章
- poj 3280(区间DP)
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7869 Accepted: 38 ...
- poj 2955 区间dp入门题
第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...
- POJ 2955 (区间DP)
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...
- POJ 1651 (区间DP)
题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...
- POJ 1141 区间DP
给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include &q ...
- POJ 1651 区间DP Multiplication Puzzle
此题可以转化为最优矩阵链乘的形式,d(i, j)表示区间[i, j]所能得到的最小权值. 枚举最后一个拿走的数a[k],状态转移方程为d(i, j) = min{ d(i, k) + d(k, j) ...
- poj 1141 区间dp+递归打印路径
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30383 Accepted: 871 ...
- POJ 3042 区间DP(费用提前计算相关的DP)
题意: 思路: f[i][j][1]表示从i到j的区间全都吃完了 现在在j点 变质期最小是多少 f[i][j][0]表示从i到j的区间全都吃完了 现在在i点 变质期最小是多少 f[i][j][0]=m ...
- POJ 2955 区间DP必看的括号匹配问题,经典例题
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14226 Accepted: 7476 Description ...
随机推荐
- Windows的cmd命令查询指定端口占用的进程并关闭
以端口8080为例: 1.查找对应的端口占用的进程:netstat -aon|findstr "8080" ,找到占用8080端口对应的程序的PID号: 2.根据PID号 ...
- 【Mysql】给mysql配置远程登录
grant all privileges on 库名.表名 to '用户名'@'IP地址' identified by '密码' with grant option; flush privileges ...
- HTML+CSS : 笔记整理(1)
meta:页面描述信息(可以在里面加入作者信息等,如: <meta name="description"content="HTML examples"&g ...
- yii rbac
一.简介 什么是rbac ? rbac是就是基于角色的访问控制. yii提供一套基础的底层接口,我们知道,rbac经历好几个阶段,从rbac0到rbac3,从基础的用户.角色.权限,到动态的rbac处 ...
- stark组件(7):增加分页功能
效果图: 分页部分代码: # 1.分页处理 all_count = self.model_class.objects.all().count() query_params = request.GET. ...
- 关于修改zeppelin的代码显示
最近我在修改zeppelin(0.7版本)的源码相关的知识,目前做的工作是修改zeppelin的代码,为了让zeppelin可以可以在页面中显示数据集,并且在其数据库中存储式真实的路径1.如果我们要运 ...
- JDBC---java与数据库中数据类型的对应关系
基础数据类型 Java 类型 SQL 类型 int 或 java.lang.Integer INTEGER long 或 java.lang.Long BIGINT short 或 java.lang ...
- 洛谷P1451 求细胞数量
求细胞数量 题目链接 这道题大概是一个最简单的联通块的题了qwq 注意枚举起点的时候 一定不要从0开始不然你就会从0进入到了其他联通块中从而多查. 一定看清题意这道题不是同色为联通块!!! AC代码如 ...
- 静态html引入js添加随机数后缀防止缓存
在web项目开发中,页面引入js被修改时,为避免浏览器缓存引起的问题,在引入js时,给js名后面加上随机数,以保证每次都发送新的请求. 在jsp中,一般通过后台取随机数即可,代码如下: <scr ...
- centos使用--centos7.3配置LNMP
目录 1 源的配置 2 安装软件 2.1 安装php7 2.2 安装nginx 2.3 安装mysql 2.4 安装vsftp (ftp登录配置) 3 开机启动设置 4 其它一些配置 4.1 git的 ...