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 ...
随机推荐
- DB2基本操作
--重启数据库 FORCE APPLICATION ALL DB2STOP DB2START --创建数据库 CREATE DATABASE mysdedb USING CODESET UTF-8 T ...
- [转]“WARNING: soft rlimits too low” in MongoDB with Mac OS X
转自:Programming and Technology If you get this warning when you connect to mongo shell in Mac OX X: * ...
- flex 4 写皮肤
皮肤容器:s:SparkSkin 主机组件: [HostComponent("spark.components.Panel")] 绘制: <s:Group left=&qu ...
- MyBatis的foreach语句详解 list array map
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,open,separator,close.it ...
- Linux内核的整体架构
Linux内核的整体架构 作者:蜗蜗 发布于:2014-2-21 13:23 分类:Linux内核分析 原文:http://www.wowotech.net/linux_kenrel/11.html ...
- 如何在User版本开启串口(Uart),抓取上层Log,开启输入控制台
[原][FAQ03891] 如何在User版本开启串口(Uart),抓取上层Log,开启输入控制台 2014-11-26阅读1369 评论0 FAQ Content [Description]如何在U ...
- linux 查看程序是否运行
命令格式:ps -ax|grep program_name 如查看包含python的程序是否运行: ps -ax|grep python ? Sl : python ToServer.py pts/ ...
- POJ 3468 (线段树 区间增减) A Simple Problem with Integers
这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...
- C++实现顺序表
#include<iostream>using namespace std; typedef int DataType; class SeqList{public: SeqList( ...
- iOS8 LaunchScreen.storyboard
我目前的需求是需要将启动图片通过LaunchScreen.storyboard 来实现. 我首先想到的是创建一个Sb,使用自动布局来布局imageview,并设置如下图: 布局好之后,在Image里 ...