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 ...
随机推荐
- Linux - bashrc之alias
1. cd ~ 2. touch .bashrc // 若该文件不存在的话 3. vim .bashrc ----------------复制粘贴如下文本--------------- # alias ...
- Linux下常用压缩 解压命令与压缩比率对比
常用的格式有:tar, tar.gz(tgz), tar.bz2, 不同方式,压缩和解压方式所耗CPU时间和压缩比率也差异也比较大. 1. tar只是打包动作,相当于归档处理,不做压缩:解压也一样,只 ...
- 记一次微信小程序在安卓的白屏问题
在做小程序的时候,做到了一个限时商品售卖,用到了倒计时,因为这个原因导致了安卓手机上使用小程序时,将小程序放入后台运行一段时间后,再次进入小程序后出现了页面白屏或者点击事件失效的情况,这里记录下 1. ...
- ES6笔记04-class的基本语法
JavaScript 语言中,生成实例对象的传统方法是通过构造函数. ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对象的模板.通过class关键字,可以定义类. clas ...
- PHP 多参数方法的重构
假设我们要完成一个保存文章的功能,如果采用函数编程的方式,大概会是下面这个样子: <?php function saveArticle($title, $content, $categoryId ...
- 从coding.net 克隆(git clone)项目代码到本地报无权限(403)错误 解决方案
直接从coding.net (git clone)项目代码到本地时,会提示没有权限的错误,如下图: 解决方案:添加远程地址的时候带上用户名及密码即可解决,格式如下: git clone http:// ...
- laravels -- Swoole加速php
LaravelS是一个胶水项目,用于快速集成Swoole到Laravel,然后赋予它们更好的性能.更多可能性. 环境 : ubuntu16 + nginx + php7.1 + LaravelS搭建高 ...
- 15、python之导入模块
一.什么是模块? 模块本质是一个py文件,我们可以通过关键字import将py文件对象导入到当前名称空间. 二.导入模块 1.import module 2.from module import ob ...
- python-3高级特征
1-切片 L = ["qinzb",'fengyong','bingyan'] L[0:3] #截取从索引0开始到索引3结束(不包含索引3) L[:3] #如果索引是从0开始则可省 ...
- win32 signal
Remarks The signal function enables a process to choose one of several ways to handle an interrupt ...