P2668 landlords and p2668 landlords

Source: Internet
Author: User

P2668 landlords and p2668 landlords
Description

Niu has recently become addicted to a poker game called landlords. Landlords are A poker game that uses 17 cards from A to K with the King of size and 54 cards. In the landlords, the card size relationship is represented as follows according to the card number: 3 <4 <5 <6 <7 <8 <9 <10 <J <Q <K <A <2 <Xiao Wang <Wang, but the color does not affect the card size. In each game, a hand card consists of n cards. Players can play cards based on the specified card type each time. First, they can win the game by playing their hands.

Now, Niu only wants to know the minimum number of cards required for several groups of his/her own. Please help him solve this problem.

It should be noted that in this question, the card type that players can play each time is slightly different from that of common landlords.

The specific rules are as follows:

Input/Output Format Input Format:

The first line contains two positive integers T and n separated by spaces, indicating the number of groups and the number of cards in each group.

Next, there are n rows of data in the T group. Each row has a non-negative integer. aibi represents a card. The digital Number of the ai card and bi represent the card color, separated by spaces. In particular, we use 1 to represent digital A, 11 to represent digital J, 12 to represent Digital Q, and 13 to represent digital K. Black peaches, red hearts, plum blossom, and square slices are represented in 1-4; the expression of the king is 01, and the expression of the king is 02.

Output Format:

A total of T rows, each row is an integer, indicating the minimum number of times the I-th card is played.

Input and Output sample Input example #1:
1 87 48 49 110 411 15 11 41 1
Output sample #1:
3
Input example #2:
1 1712 34 32 35 410 23 312 20 11 310 16 212 111 35 212 42 27 2
Output sample #2:
6
Description

Example 1

There are A total of 1 group of cards, including 8 cards: Side 7, side 8, black peach 9, side 10, black peach J, black peach 5, side A and black peach. You can play A single sub-card (side 7, side 8, black peach 9, side 10, black peach J), A single card (black peach 5), and A sub-card (black peach A and side) in 3 times.

For different test points, we agree that the number of cards T and n are as follows:

Data guarantee: all cards are randomly generated.

 

Nima Guang search 323 lines 85 points, Deep Search pressure after 57 lines on ,,,'

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 const int MAXN = 23; 7 int read (int & n) 8 {9 char c = '-'; int x = 0; 10 while (c <'0' | c> '9 ') c = getchar (); 11 while (c> = '0' & c <= '9') 12 {13 x = x * 10 + (c-48 ); 14 c = getchar (); 15} 16 n = x; 17} 18 int T, n, p, hs; 19 int ans; 20 int card_num [MAXN]; // record the appearance times of each digital type 21 int happen [MAXN]; // The number of occurrences of the number of records 22/* For example 3 appears twice, A appears twice, then, happen [2] = 2 */23 int take_num [5] = {,}; // The rules for landlords, corresponding to single-shun, double-shun, and triple-shun 24 void dfs (int now) // now indicates the number of operations that have been performed 25 {26 if (now> ans) 27 return; // pruning 28 memset (happen, 0, sizeof (happen); 29 for (int I = 0; I <= 14; I ++) 30 happen [card_num [I] ++; 31 int cs = 0; // NUMBER OF CURRENT ROUND operations 32 while (happen [4]) // Band 33 {34 cs ++; 35 happen [4] --; 36 if (happen [2]> = 2) // Based on greedy principle, if two cards are available, then no 37 happen [2]-= 2; // you can bring two pairs of cards without one pair of cards. 38 else if (happen [1]> = 2) 39 happen [1]-= 2; // four cards each time can carry two single cards 40} 41 while (happen [3]) 42 {43 cs ++; 44 happen [3] --; 45 if (happen [2]) 46 happen [2] --; 47 else if (happen [1]) 48 happen [1] --; // same idea as above. Only one 49} 50 if (card_num [0] & card_num [1] & happen [1]> = 2) can be carried for three cards) 51 cs --; // when kingdom and kingdom can both be used as a pair card, then 52 // + happen [1] is required in the following statement. therefore, the default value is King Wang. When a single card is 53 // At the same time, King Wang needs two operations. In fact, one operation can be completed, equivalent to 2-1 = 1 54 cs = cs + happen [1] + happen [2]; 55 // The remaining pairs and single cards need to be operated once in each group 56 ans = min (ans, cs + now); // update answer 57 for (int k = 1; k <= 3; k ++) // k indicates the type of subose, 1: single shun 2: Double shun 3: Three shun 58 {59 for (int I = 3, j; I <= 14; ++ I) // enumerate each card. Because 2 cannot appear in the subfolder, the 60 {61 for (j = I; card_num [j]> = k & j <= 14; ++ j) 62 {// search for shunzi 63 card_num [j]-= k in a feasible condition and range; // subtract first, and then 64 if (j-I + 1> = take_num [k]) will be added later. // The 65 dfs (now + 1) can be obtained along with the downstream ); // just remove 66} 67 while (j> I) // Recursive Backtracking 68 card_num [-- j] + = k; 69} 70} 71 72} 73 int main () 74 {75 read (T); read (n); 76 while (T --) 77 {78 memset (card_num, 0, sizeof (card_num); 79 ans = n; 80 for (int I = 1; I <= n; I ++) 81 {82 read (p ); read (hs); 83 if (p = 0) card_num [hs-1] ++; 84 // the king as 0, King as 1. ensure that the card_num array does not conflict with 85 else if (p = 1) card_num [14] ++; // regard A as 14 86 else card_num [p] ++; 87} 88 89 dfs (0); 90 printf ("% d \ n", ans); 91} 92 return 0; 93}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.