Description
Niuniu recently got hooked on a poker game called the Landlord. A bucket landlord is a poker game that uses spades, hearts, plum blossoms, square pieces of a to K plus a total of 54 cards of the king's size. In the bucket landlord, the size of the card is based on the digital representation of the following:3<4<5<6<7<8<9<10<j<q<k<a<2< Xiao Wang < king, And the color does not affect the size of the card. In each game, a second hand is made up of n cards. Each time the player can play according to the prescribed card type, first of all to light their own hand to win the game. Now, cattle and cows just want to know, for their own group of hands, the minimum number of times required to play the cards can be light. Please help him to solve the problem. It is important to note that the players in the subject can be shot each time the card type and the general landlords similar and slightly different. The specific rules are as follows:
Input
The first line contains 2 positive integer t,n separated by spaces, representing the number of groups of hands and the number of cards per group.
Next T Group of data, each set of data n rows, a non-negative integer to Ai,bi, each line represents a card, wherein the AI represents the card of the digital, Bi represents the color of the card, the middle with a space separated. In particular, we use one of the digital a,11 said Digital j,12 said digital q,13 mean digital K, spades, hearts, plum, square slices respectively with 1-4 to express; Xiao Wang's expression method is 01, the King's expression method is 02. Output
A total of T-lines, one integer per line, indicating the minimum number of times to hit a group of hands.
Sample Input1 8
7 4
8 4
9 1
10 4
11 1
5 1
1 4
1 1
Sample Output3HINT
There are 1 groups of hands, including 8 cards: Square 7, Square 8, spades 9, square piece 10, Spades J, Spades 5, Square
Slice A and spades a. Can be played by Tanshong (Square 7, Square 8, Spades 9, Square 10, Spades J), a single card (spades 5) and a pair of cards (spades A and square a) within 3 times to light. T<=10n<=23 Well, not pruning also fast.
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intn= the;6 intCnt[n];7 intDFS (intD=0){8 intflag=0, ret= -;9 for(intI=0; i<= -; i++)Ten if(Cnt[i]) flag=1; One if(!flag)return 0; A for(intI=3; i<=Ten; i++){ -flag=1; - for(intj=i;j<=i+3; j + +)if(!cnt[j]) flag=0; the if(!flag)Continue; - for(intj=i;j<=i+3; j + +) cnt[j]-=1; - for(intj=i+4; j<= the; j + +){ - if(Cnt[j]) {cnt[j]-=1; Ret=min (Ret,dfs () +1);} + Else{ for(intk=j-1; k>=i;k--) cnt[k]+=1; Break;} - } + } A at for(intI=3; i<= A; i++){ -flag=1; - for(intj=i;j<=i+1; j + +)if(cnt[j]<2) flag=0; - if(!flag)Continue; - for(intj=i;j<=i+1; j + +) cnt[j]-=2; - for(intj=i+2; j<= the; j + +){ in if(cnt[j]>=2) {cnt[j]-=2; Ret=min (Ret,dfs () +1);} - Else{ for(intk=j-1; k>=i;k--) cnt[k]+=2; Break;} to } + } - the for(intI=3; i<= -; i++){ *flag=1; $ for(intj=i;j<=i;j++)if(cnt[j]<3) flag=0;Panax Notoginseng if(!flag)Continue; - for(intj=i;j<=i;j++) cnt[j]-=3; the for(intj=i+1; j<= the; j + +){ + if(cnt[j]>=3) {cnt[j]-=3; Ret=min (Ret,dfs () +1);} A Else{ for(intk=j-1; k>=i;k--) cnt[k]+=3; Break;} the } + } - $ //three bands 13 with two $ for(intI=0; i<= -; i++){ - if(cnt[i]<3)Continue; -cnt[i]-=3; the for(intj=1; j<= -; j + +){ - if(I==J)Continue;Wuyi if(cnt[j]>=2) {cnt[j]-=2; Ret=min (Ret,dfs () +1); cnt[j]+=2;} the if(Cnt[j]) {cnt[j]-=1; Ret=min (Ret,dfs () +1); cnt[j]+=1;} - } Wucnt[i]+=3; - } About $ for(intI=0; i<= -; i++){ - if(cnt[i]<4)Continue; -cnt[i]-=4; - for(intj=1; j<= -; j + +)if(i!=j&&cnt[j]>1) A for(intk=j+1; k<= -; k++)if(i!=k&&cnt[k]>1) +{cnt[j]-=2; cnt[k]-=2; Ret=min (Ret,dfs () +1); cnt[j]+=2; cnt[k]+=2;} the - for(intj=1; j<= -; j + +)if(i!=j&&Cnt[j]) $ for(intk=j+1; k<= -; k++)if(i!=k&&Cnt[k]) the{cnt[j]-=1; cnt[k]-=1; Ret=min (Ret,dfs () +1); cnt[j]+=1; cnt[k]+=1;} thecnt[i]+=4; the } theflag=0; - for(intI=0; i<= -; i++)if(Cnt[i]) flag+=1; in returnmin (ret,flag); the } the intT,n; About intMain () { theFreopen ("landlords.in","R", stdin); theFreopen ("Landlords.out","W", stdout); thescanf"%d%d",&t,&n); + while(t--){ -memset (CNT,0,sizeof(CNT)); the for(intI=1, a,b;i<=n;i++){Bayiscanf"%d%d",&a,&b); thecnt[a]+=1; the } -cnt[ -]=cnt[1];cnt[1]=0; -printf"%d\n", DFS (1)); the } the return 0; the}
Basic algorithm (search): NOIP 2015 landlords