Bucket landlord
si lu : Note a as the first card when reading , because it can be connected to K ,
The general idea is first out bomb and four with two and three with a and then I'm done with the cards and the cards . Record and update Answer, after enumerating the straights, and continuing to search down.
Note: To understand test instructions, the topic description is not very clear .... In addition, I feel that the color of the cards can only be used to distinguish the size of king. In addition, you can use greed before the whole straight. Other similar four-band two, three-band cards
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intMax = -;intT, N, Answer;intcard [Max];intCount [Max];voidDFS (intX) { if(X > Answer)return; intRest =0; memset (Count,0,sizeof(Count)); for(inti =0; I <= -; i++) Count [card[i]]++; for(; Count [4]; ) {Count [4]--;//bombsrest++; if(Count [2] >=2)//Judging if you can take 2 .Count [2] -=2; Else if(Count [1] >=2) Count [1] -=2; } for(; Count [3]; ) {Count [3]--;//Three cardsrest++; if(Count [2])//can you bring aCount [2]--; Else if(Count [1]) Count [1]--; } if(Card [0] && Card [1] && Count [1] >=2)//See if the rest of the cards are king-fried.rest--; Rest+ = Count [1] + Count [2]; Answer= min (rest +X, Answer); for(inti =3, J; I <= -; ++i)//Tanshong { for(j = i; card [j] && J <= -; ++j) {card [j]--; if(J-i +1>=5)//if more than five cards are left, search downDFS (X +1); } for(; J >i;) Card [--j]++; } for(inti =3, J; I <= the; ++i)//Double CIS { for(j = i; card [j] >=2&& J <= -; ++j) {card [j]-=2; if(J-i +1>=3) DFS (X+1); } for(; J >i;) Card [--J] + =2; } for(inti =3, J; I <= the; ++i)//three shun son { for(j = i; card [j] >=3&& J <= -; ++j) {card [j]-=3; if(J-i +1>=2) DFS (X+1); } for(; j>i;) Card [--J] + =3; }}intMain () {CIN>> T >>N; intA, B; while(t--) {memset (card,0,sizeof(card)); Answer=N; for(inti =1; I <= N; i++) {cin>> A >>B; if(A = =0) Card [B-1]++;//The king is to be kept apart, because they cannot form a pair, and cannot be Else if(A = =1) Card [ -]++; ElseCard [a]++; } DFS (0); cout<< Answer <<Endl; } return 0;}
Bucket landlord (NOIP2015 Day1 T3)