10118 - Free CandiesTime limit: 30.000 seconds |
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 5candies. Each time, he puts a candy at the top of one pile into the basket, and if there're two candiesof 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 isplayed 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 #2Piles Basket Pocket Piles Basket Pocket1 2 3 4 1 3 41 5 6 7 1 5 6 72 3 3 3 nothing nothing 2 3 3 3 2 nothing4 9 8 6 4 9 8 68 7 2 1 8 7 2 1Step3 Take one from pile #2 Step4 Take one from pile #3Piles Basket Pocket Piles Basket Pocket1 3 4 1 41 6 7 1 6 72 3 3 3 2 5 nothing 2 3 3 3 2 3 5 nothing4 9 8 6 4 9 8 68 7 2 1 8 7 2 1Step5 Take one from pile #2 Step6 Put two candies into his pocketPiles Basket Pocket Piles Basket Pocket1 4 1 41 6 7 1 6 72 3 3 2 3 3 5 nothing 2 3 3 2 5 a pair of 34 9 8 6 4 9 8 68 7 2 1 8 7 2 1Note 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 atmost?InputThe input will contain not more than 10 test cases. Each test case begins with a line containing a singleinteger n(1 n 40) representing the height of the piles. In the following n lines, each line containsfour integers xi1, xi2, xi3, xi4(in the range 1..20). Each integer indicates the color of the correspondingcandy. The test case containing n = 0 will terminate the input, you should not give an answer to thiscase.OutputOutput the number of pairs of candies that the cleverest little child can take home. Print your answerin a single line for each test case.Sample Input51 2 3 41 5 6 72 3 3 34 9 8 68 7 2 111 2 3 431 2 3 45 6 7 81 2 3 40Sample Output803
此题状态很容易想到,但是不得不说我的记忆化搜索真的很渣,写完之后改了好久,问题主要出在递归上,我不知道该怎么表示了。由于记忆化搜索理解的不好,我这题几乎是半猜半蒙弄出来的,待我把记忆化搜索弄清楚再回来看它。
#include#include #include #include #include #include #include #include #include