题意:

有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的更多相关文章

  1. 【POJ2886】Who Gets the Most Candies?-线段树+反素数

    Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...

  2. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  3. Who Gets the Most Candies?(线段树 + 反素数 )

    Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d &am ...

  4. poj---(2886)Who Gets the Most Candies?(线段树+数论)

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Acc ...

  5. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...

  6. HDU 5127 Dogs' Candies

    Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...

  7. C. Om Nom and Candies 巧妙优化枚举,将复杂度控制在10e6

    C. Om Nom and Candies 无线超大背包问题 #include <iostream> #include <cstdio> #include <cstrin ...

  8. POJ 3159 Candies (栈优化spfa)

    Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...

  9. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  10. (poj)3159 Candies

    题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...

随机推荐

  1. MongoDB驱动特性检查列表

    http://docs.mongodb.org/meta-driver/latest/legacy/feature-checklist-for-mongodb-drivers/ 1. 基本(Essen ...

  2. ThinkPHP:读取数据库数据 (2)

    项目配置文件Conf/config.php中添加数据库连接信息: // 添加数据库配置信息 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'loc ...

  3. 317. Shortest Distance from All Buildings

    题目: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where th ...

  4. SQL SERVER ->> Wait Stats

    等待状态是隔离SQL SERVER性能问题的一个重要手段,从实例级别入手.可以大概看出SQL SERVER在过去某段时间内是否处于某种资源的吃紧状态.不过取决于你是否有建立有效的性能数据收集机制.sy ...

  5. VI操作--跳到最后一行和跳到最后一行的最后一个字符

    vi操作 1.跳到文本的最后一行:按“G”,即“shift+g” 2.跳到最后一行的最后一个字符 : 先重复1的操作即按“G”,之后按“$”键,即“shift+4”. 3.跳到第一行的第一个字符:先按 ...

  6. 《大道至简-Team》

    已经学习了<大道至简>两章,我们了解了编程的本质和“懒人”造就了方法.书中没有提供给我们编程的技巧,捷径,而是从别的方面为我们讲解了编程的精义.第三章就为我们引入了“团队”这个概念. 我们 ...

  7. YTU 2614: A代码完善--系统日期

    2614: A代码完善--系统日期 时间限制: 1 Sec  内存限制: 128 MB 提交: 216  解决: 113 题目描述 注:本题只需要提交填写部分的代码,请按照C++方式提交. 已知某操作 ...

  8. 在DirectX 中进行2D渲染

    http://flcstudio.blog.163.com/blog/static/756035392008115111123672/ 最近,我看到很多关于DirectX8在最新的API中摒弃Dire ...

  9. .gitignore规则不生效的解决办法

    .gitignore规则不生效的解决办法 使用git 的时候,在.gitignore中已经添加了某个文件或者文件夹,但是使用git status还能看见该文件的修改提示--–说明.gitignore未 ...

  10. Qt之国际化

    简介 Qt国际化属于Qt高级中的一部分,本想着放到后面来说,上节刚好介绍了Qt Linguist,趁热打铁就一起了解下. 对于绝大多数的应用程序,在刚启动时,需要加载默认的语言(或最后一次设置的语言) ...