Problem B. Free Candies
Problem B. Free Candies |
The Problem
Little Bob is playing a game. He wants to win some candies in it - as many as possible.
There are 4 piles, each pile contains N candies. Bob is given a basket which can hold at most 5 candies. Each time, he puts a candy at the top of one pile into the basket, and if there're two candies of the same color in it ,he can take both of them outside the basket and put them into his own pocket. When the basket is full and there are no two candies of the same color, the game ends. If the game is played perfectly, the game will end with no candies left in the piles.
For example, Bob may play this game like this (N=5):
Step1 | Initial Piles | Step2 | Take one from pile #2 | ||
Piles | Basket | Piles | Basket | ||
1 2 3 4 1 5 6 7 2 3 3 3 4 9 8 6 8 7 2 1 | nothing | nothing | 1 3 4 1 5 6 7 2 3 3 3 4 9 8 6 8 7 2 1 | 2 | nothing |
Step3 | Take one from pile #2 | Step4 | Take one from pile #3 | ||
Piles | Basket | Piles | Basket | ||
1 3 4 1 6 7 2 3 3 3 4 9 8 6 8 7 2 1 | 2 5 | nothing | 1 4 1 6 7 2 3 3 3 4 9 8 6 8 7 2 1 | 2 3 5 | nothing |
Step5 | Take one from pile #2 | Step6 | put two candies into his pocket | ||
Piles | Basket | Piles | Basket | ||
1 4 1 6 7 2 3 3 4 9 8 6 8 7 2 1 | 2 3 3 5 | nothing | 1 4 1 6 7 2 3 3 4 9 8 6 8 7 2 1 | 2 5 | a pair of 3 |
Note that different numbers indicate different colors, there are 20 kinds of colors numbered 1..20.
'Seems so hard...'Bob got very much puzzled. How many pairs of candies could he take home at most?
The Input
The input will contain no more than 10 test cases. Each test case begins with a line containing a single integer n(1<=n<=40) representing the height of the piles. In the following n lines, each line contains four integers xi1,xi2,xi3,xi4 (in the range 1..20). Each integer indicates the color of the corresponding candy. The test case containing n=0 will terminate the input, you should not give an answer to this case.
The Output
Output the number of pairs of candies that the cleverest little child can take home. Print your answer in a single line for each test case.
Sample Input
5 1 2 3 4 1 5 6 7 2 3 3 3 4 9 8 6 8 7 2 1 1 1 2 3 4 3 1 2 3 4 5 6 7 8 1 2 3 4 0
Sample Output
8 0 3
这道题网上找的题解都不是令人很满意,题解都是用一个4维dp数组记忆化搜索,那为什么4维够了呢?篮子里的糖果难道不算一维状态吗?是不是要加一维呢?
当然加一维肯定是没错的,但是数组开不下这么大啊,那么只能祈求4维了,但4维为什么是对的呢?
我们不妨换个视角,试问一个问题,从四个堆分别取a,b,c,d个糖果,那么无论怎么取,篮子里剩下的糖果是否只可能有一种状态呢?
答案是显然的,因为从a,b,c,d里划去两两相同的一对对糖果,剩下的就是在篮子里的了,因此篮子中的状态肯定是确定的了,当然前提是中间取的过程中篮中的糖果不超过5个,否则即使a,b,c,d无法划去两两相同的一对对糖果,因为在中间取的过程已发生不合法的情况,只可能划掉一部分了。但这还是说明了只要能够达到某种状态,那么该状态对应篮中的糖果肯定是一样的,因为篮中的糖果不区分顺序。但如果篮中的糖果区分顺序,这题就和取法有关系,那就无法做了。理解了这个,记忆化搜索就不成问题了,用一个int记录篮中的状态。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int pile[4][50],top[4];
int dp[50][50][50][50];
int n;
int dfs(int num,int status){
if(num==5) return 0;
int &ans=dp[top[0]][top[1]][top[2]][top[3]];
if(ans!=-1) return ans;
ans=0;
for(int i=0;i<4;i++){
if(top[i]==n) continue;
++top[i];
if(status&1<<pile[i][top[i]])
ans=max(ans,dfs(num-1,status^1<<pile[i][top[i]])+1);
else
ans=max(ans,dfs(num+1,status^1<<pile[i][top[i]]));
--top[i];
}
return ans;
}
int main()
{
while(scanf("%d",&n),n){
for(int j=1;j<=n;j++)
for(int i=0;i<4;i++)
scanf("%d",&pile[i][j]);
memset(top,0,sizeof top);
memset(dp,-1,sizeof dp);
printf("%d\n",dfs(0,0));
}
return 0;
}