poj 1390 Blocks (记忆化搜索)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 4318 | Accepted: 1745 |
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
1~n.
Output
Sample Input
2
9
1 2 2 2 2 3 3 3 1
1
1
Sample Output
Case 1: 29
Case 2: 1
递归形式的动态规划:dp[st][ed][len]从st到ed全然消除。且ed右边挨着有一个len的大块颜色和ed同样.
一种消除方式是,Len块直接和ed块合并直接消除得到分数work(st,ed-1,0)+(a[ed].n+len)*(a[ed].n+len);
还有一种是在st到ed之间找到一个块p和ed块颜色同样,把这3块直接合并 work(st,p,a[ed].n+len)+work(p+1,ed-1,0);
两种方式取最大的值。
当st==ed时递归结束。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
#define LL __int64
#define N 210
const int inf=0x1f1f1f1f;
struct node
{
int c,n,p;
}a[N];
int f[N][N][N];
int work(int st,int ed,int len)
{
if(f[st][ed][len])
return f[st][ed][len];
int i,ans=(a[ed].n+len)*(a[ed].n+len);
if(st==ed)
{
f[st][ed][len]=ans;
return ans;
}
ans+=work(st,ed-1,0);
for(i=ed-1;i>=st;i--)
{
if(a[i].c!=a[ed].c)
continue;
int tmp=work(st,i,a[ed].n+len)+work(i+1,ed-1,0);
if(tmp<=ans)
continue;
ans=tmp;
break;
}
f[st][ed][len]=ans;
return ans;
} int main()
{
int T,t,cnt,i,n,Cas=1;
scanf("%d",&T);
while(T--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
scanf("%d",&t);
cnt=0;
a[cnt].c=t;
a[cnt].n=1;
for(i=1;i<n;i++)
{
scanf("%d",&t);
if(t==a[cnt].c)
{
a[cnt].n++;
}
else
{
cnt++;
a[cnt].c=t;
a[cnt].n=1;
}
}
memset(f,0,sizeof(f));
printf("Case %d: %d\n",Cas++,work(0,cnt,0));
}
return 0;
}
poj 1390 Blocks (记忆化搜索)的更多相关文章
- POJ 1390 Blocks(记忆化搜索+dp)
POJ 1390 Blocks 砌块 时限:5000 MS 内存限制:65536K 提交材料共计: 6204 接受: 2563 描述 你们中的一些人可能玩过一个叫做“积木”的游戏.一行有n个块 ...
- POJ 1088 DP=记忆化搜索
话说DP=记忆化搜索这句话真不是虚的. 面对这道题目,题意很简单,但是DP的时候,方向分为四个,这个时候用递推就好难写了,你很难得到当前状态的前一个真实状态,这个时候记忆化搜索就派上用场啦! 通过对四 ...
- POJ 1088 滑雪 (记忆化搜索)
题目链接:http://poj.org/problem?id=1088 题意很好懂,就是让你求一个最长下降路线的长度. dp[i][j]记录的是i j这个位置的最优的长度,然后转移方程是dp[i][j ...
- POJ 1088 滑雪(记忆化搜索+dp)
POJ 1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 107319 Accepted: 40893 De ...
- 专题1:记忆化搜索/DAG问题/基础动态规划
A OpenJ_Bailian 1088 滑雪 B OpenJ_Bailian 1579 Function Run Fun C HDU 1078 FatMouse and Chee ...
- POJ 1088 滑雪(记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 92384 Accepted: 34948 Description ...
- poj 3249 Test for Job (DAG最长路 记忆化搜索解决)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8990 Accepted: 2004 Desc ...
- (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
http://poj.org/problem?id=3186 Description FJ has purchased N (1 <= N <= 2000) yummy treats ...
- poj 3249(bfs+dp或者记忆化搜索)
题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...
随机推荐
- ES6特性之模块【Modules】
ES6之前已经出现了js模块加载的方案,最主要的是CommonJS和AMD规范.commonjs主要应用于服务器,实现同步加载,如nodejs.AMD规范应用于浏览器,如requirejs,为异步加载 ...
- Android开发笔记(4)——MainActivity.java文件修改&布局嵌套
笔记链接:http://www.cnblogs.com/igoslly/p/6805020.html 笔记以开发名为CoffeeOrder的app活动为线索,介绍app如何从功能设计→ ...
- Farseer.net轻量级开源框架 入门篇:Where条件的终极使用
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 查询数据详解 下一篇:Farseer.net轻量级开源框架 中级篇: 事务的使用 ...
- dom4j使用方法详解
本文先做知识点的简单介绍,最后附完整案例. 一.解析XML文件 public class Foo { //url为XML文档地址 //自己封装了一个工具类 返回解析完成的document public ...
- ThinkPHP---插件highcharts
[一]概论 (1)介绍 基于jquery开发的国外图标插件,统计图,折线图,饼状图等常常用到. 国内也有一款类似插件echarts,由百度开发. (2)官网:www.highcharts.com ...
- ubuntu 16.04 添加网卡
root@ubuntu:~# ls /sys/class/net/ enp0s3 enp0s8 lo root@ubuntu:~# vim /etc/network/interfaces # This ...
- 支持向量机(SVM)原理浅析
因为网页博客输入公式很麻烦,所以就在word上面写了,然后截图发上来. 后续关于SVM和FC在深度学习当中得使用对比分析,我再补充.
- 洛谷——P4296 [AHOI2007]密码箱
P4296 [AHOI2007]密码箱 密码x大于等于0,且小于n,而x的平方除以n,得到的余数为1. 求这个密码,$1<=n<=2,000,000,000$ 暴力枚举,数据有点儿水$O( ...
- 解决idea控制台打印乱码问题
idea控制台打印乱码,用起来总别扭,也是在网上搜索了一番,靠一点猜测解决了. 首先打开你自己的idea的安装目录下(即右键桌面图标,点击打开文件所在位置),然后找到idea.exe.vmoption ...
- TestNG超时测试
用@Test(timeOut = XXX) 指定超时时间,单位是毫秒 package com.janson; import org.testng.annotations.Test; public cl ...