Description
Mahjong is one of the traditional entertainment tools in China. Mahjong card can be divided into cards (a total of East, south, west, north, middle, fat, white seven kinds) and the number of cards (divided into a sliver, pancake, thousand three kinds of suits, each suit has one to nine nine kinds of cards), each of the four cards. In Mahjong, usually a group of cards (i.e. the finished card) is made up of 14 cards. 14 cards in two pairs (that is, the same two cards), the remaining 12 are composed of three groups of four groups, each group must be a straight (that is, the same suit and ordinal number of the number of cards, such as the three, four or five) or the inscription (that is, the exact same three cards). A set of cards refers to a group of 13 cards, and plus a certain card can be composed and cards. That plus card can be called a waiting card. Here, we consider a special kind of mahjong. In this special kind of mahjong, there is no word card, there is only one color. However, the ordinal number is not limited to a range of one to nine, but within the range of 1 to N. At the same time, there is no limit of four cards per card. A set of cards made up of 3m + 2 cards, of which two are made up of pairs, the remaining 3m are composed of three groups of M group, each group shall be straight or engraved. Now give a set of 3m + 1 cards, ask to determine whether the group card is a draw (that is, one can and cards). If so, output all possible waiting cards.
Input
contains two lines. The first line consists of two integers separated by spaces N, M (9<=n<=400, 4<=m<=1000). The second line contains 3m + 1 integers separated by spaces, each within a range of 1 to N. These numbers represent the number of cards that are required to judge the draw.
Output
The output is one line. If the deck is a draw, all the possible waiting cards are output, separated by a space. All ordinals must be output in order from small to large. If the deck is not a draw, the output is "NO".
Sample Input9 4
1 1 2 2 3 3 5 5 5 7 8 8 8Sample Output6 7 9Ideas
First see this question: Yes! I've seen it in great white books! Then decisively wrote a burst search, TLE, stroke.
Then think for a while search optimization, found still no solution.
Anger to see the puzzle, found that I really weak explosion.
Or first enumerate the cards to join the hand.
Then enumerate the sparrow Head (pair) and subtract from the hand.
Determine if the and card is O (n).
For each card, first put it mod 3, compose the inscription.
Then the rest of the cards make a straight forward. There's no more.
Correctness is obvious.
There are two things to note:
One, can not subtract the bird head after all the cards are mod 3, otherwise it will lead to similar to the three-step high (123,234,345) in the 3 into 0, no solution.
Second, no solution remember output no
And then.. There seems to be no more.
I'm not going to tell you that I'm doing this because I've been working on a Japanese hemp lately. ←_←
1#include <iostream>2#include <cstring>3#include <string>4#include <cstdio>5#include <cstdlib>6#include <cmath>7#include <algorithm>8#include <queue>9#include <stack>Ten#include <map> One#include <Set> A#include <list> -#include <vector> -#include <ctime> the#include <functional> - #definePRITNF printf - #defineScafn scanf - #defineSACNF scanf + #definefor (i,j,k) for (int i= (j); i<= (k);(i) + +) - #defineClear (a) memset (A,0,sizeof (a)) + using namespacestd; Atypedef unsignedintUint; at Const intinf=0x3fffffff; - ///==============struct declaration============== - - ///==============var declaration================= - Const intmaxn= the; - intn,m; in intMJ[MAXN],REM[MAXN]; - ///==============function declaration============ to BOOLHu (); + ///==============main code======================= - intMain () the { * #definefile__ $ #ifdef file__Panax NotoginsengFreopen ("input","R", stdin); -Freopen ("Output","W", stdout); the #endif +scanf"%d%d",&n,&m); A for(intI=1; i<=3*m+1; i++){ the intV;SCANF ("%d",&v); +mj[v]++; - } $memcpy (REM,MJ,sizeof(REM)); $ BOOLfirst=true; - for(intI=1; i<=n;i++){ - for(intj=1; j<=n;j++){ thememcpy (Mj,rem,sizeof(REM)); mj[i]++; - if(mj[j]<2)Continue; mj[j]-=2;Wuyi if(Hu ()) { the if(!First ) -printf"%d", i); Wu Else -printf"%d", i), first=false; About Break; $ } - } - } - if(first) printf ("no\n"); A return 0; + } the ///================fuction code==================== - BOOLHu () { $ for(intI=1; i<=n+2; i++){ the if(mj[i]<0)return false; themj[i]%=3; themj[i+1]-=mj[i]; mj[i+2]-=Mj[i]; the } - return true; in}Bzoj 1028
"JSOI2007" Mahjong Bzoj 1028