Free Candies
题意:
有4堆东西,每堆有n个每个有一个颜色,现在有一个篮子最多能装5个不同的颜色的东西,每次都从堆顶拿,当篮子出现两个相同颜色,可以获得这两个东西,求获得的最大数量
分析:
因为就4推,可以把各堆的取得状态表示出来,用记忆化搜索,因为最多可以装5个作为转移的状态。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int pile[][],top[],dp[][][][],n;
int dfs(int num,int color[]){
if(dp[top[]][top[]][top[]][top[]]!=-)return dp[top[]][top[]][top[]][top[]];
if(num==)
return dp[top[]][top[]][top[]][top[]]=;
int maxv=;
for(int i=;i<;++i){
if(top[i]==n)continue;
int tmp=pile[i][top[i]];
top[i]+=;
if(color[tmp]){
color[tmp]=;
maxv=max(maxv,dfs(num-,color)+);
color[tmp]=;
}
else{
color[tmp]=;
maxv=max(maxv,dfs(num+,color));
color[tmp]=;
}
top[i]-=;
}
return dp[top[]][top[]][top[]][top[]]=maxv;
}
int main()
{
int color[];
while(~scanf("%d",&n)&&n){
for(int i=;i<n;++i)
for(int j=;j<;++j)
scanf("%d",&pile[j][i]);
memset(dp,-,sizeof(dp));
memset(top,,sizeof(top));
memset(color,,sizeof(color));
printf("%d\n",dfs(,color));
}
return ;
}
Free Candies的更多相关文章
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- Who Gets the Most Candies?(线段树 + 反素数 )
Who Gets the Most Candies? Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d &am ...
- poj---(2886)Who Gets the Most Candies?(线段树+数论)
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10373 Acc ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- HDU 5127 Dogs' Candies
Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
- C. Om Nom and Candies 巧妙优化枚举,将复杂度控制在10e6
C. Om Nom and Candies 无线超大背包问题 #include <iostream> #include <cstdio> #include <cstrin ...
- POJ 3159 Candies (栈优化spfa)
Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- (poj)3159 Candies
题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...
随机推荐
- Delphi对于控件的SuperClassing(封装并扩展Button,使之变成TButton)
写博客写了这么久,但是一直不知道应该怎么样写函数之间的调用关系和执行顺序,因为不停的跳来跳去的,但是写的时候却只能顺序写调用关系,直到今天发现这种写法很不错: TButton创建窗口是在CreateW ...
- Struts2笔记——Action校验器
在struts2中,我们可以实现对action的所有方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1.采用手工编写代码实现. 2.基于XML配置 ...
- http://my.oschina.net/pangyangyang/blog/144495
http://my.oschina.net/pangyangyang/blog/144495
- 利用Apriori算法对交通路况的研究
首先简单描述一下Apriori算法:Apriori算法分为频繁项集的产生和规则的产生. Apriori算法频繁项集的产生: 令ck为候选k-项集的集合,而Fk为频繁k-项集的集合. 1.首先通过单遍扫 ...
- 解决javascript加减乘除及toFixed的误差问题
//用于替换原有的toFixed,解决精度误差问题 Number.prototype.myToFixed=function(s){ if(s == null){s = 0;} var value = ...
- HttpClient通过GET和POST获取网页内容
中国银行支付网关---银行回调的接口 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面 /** * 中国银行支付网关---银行回调的接口 * @svncode svn://10. ...
- 我的ipad应用备份
听歌: 网易云音乐,QQ音乐 摄影: CameraConnect,Replay,PhotoBooth,PhotoCollage,画中画相机,Creative Shape, 简拼,玩图,PicsArt, ...
- Oracle数据库之一
数据库(oracle) SQL --结构化查询语言,所有数据库基本通用 数据库是数据的仓库,用于存储数据. 内存.文件和数据库区别 内存中的数据是非持久的,关机之后就会消失. 文件和数据库中的数据都是 ...
- 函数lock_rec_get_n_bits
/*********************************************************************//** Gets the number of bits i ...
- [转载]流式大数据处理的三种框架:Storm,Spark和Samza
许多分布式计算系统都可以实时或接近实时地处理大数据流.本文将对三种Apache框架分别进行简单介绍,然后尝试快速.高度概述其异同. Apache Storm 在Storm中,先要设计一个用于实时计算的 ...