Mahjong sorting time limit:1 Second Memory limit:65536 KB
Dreamgrid has just found a set of mahjong with suited tiles and a white Dragon the in his pocket. Each suited tile has a suit (Character, Bamboo or Dot) and a rank (ranging from 1 to), and there is exactly one tile of E Ach rank and suit combination.
Character tiles whose rank ranges from 1 to 9
Bamboo tiles whose rank ranges from 1 to 9
Dot tiles whose rank ranges from 1 to 9
White Dragon Tile
As Dreamgrid is bored, he decides to play with these tiles. He-selects one of the suited tiles as the "lucky Tile", then he picks tiles from the set of tiles and RTS tiles with the following rules:
The "lucky tile", if contained in the tiles, must is placed in the leftmost position.
For two tiles and such this neither of them is the "lucky tile", if
is a Character tile and is a Bamboo tile, or
is a Character tile and is a Dot tile, or
is a Bamboo tile and is a Dot tile, or
and have the same suit and the rank of is smaller than the rank of, then must being placed to the left of.
White Dragon Tile is a special tile. If it ' s contained in the tiles, it's considered as the original (Not-lucky) version of the lucky tile during. For example, consider the following sorted tiles, where "3 Character" is selected as the lucky. White Dragon Tile, in this case, are considered to being original not-lucky version of "3 Character" and should be placed Between "2 Character" and "4 Character".
As Dreamgrid is quite forgetful, he immediately forgets what the lucky tile are after the sorting! Given sorted tiles, please tell Dreamgrid the number of possible lucky. Input
There are multiple test cases. The ' The ' input contains an integer, indicating the number of test cases. For each test case:
The I line contains two integers and (,), indicating the number of sorted tiles and the maximum rank of suited tile S.
For the next lines, the-th line describes the-th sorted tile counting from left to right. The line begins with a capital letter (), indicating the suit of the-th tile:
If, then an integer () follows, indicating that it ' s a Character tile with rank;
If, then an integer () follows, indicating that it ' s a Bamboo tile with rank;
If, then an integer () follows, indicating the IT ' s a Dot tile with rank;
If, then it ' s a white drangon tile.
It ' s guaranteed that there exists at least one possible lucky, and the sum of the in all test tile cases ' t doesn. Output
For each test case output one line containing one integer, indicating the number of possible lucky tiles. Sample Input
4
3 9
C 2
W
C 4 6
9
C 2
C 7
w
B 3
b 4
D 2
3
C 2
w
C 9
3 9
C 1
B 2
D 3
Sample Output
2
4
7
25
Hint
For the "2 Character" and "3 Character" are possible lucky tiles.
For the second sample, "8 Character", "9 Character", "1 Bamboo" and "2 Bamboo" are possible lucky.
To give a 3*m card, there is a special card, first from the 3*m card to pick out a lucky card, then in the 3*m + 1 card in the selection of n card, if this lucky card in the N card, put him to the left, the remaining cards according to the title of the order, if that a special card also exists in the N card , put this card in the lucky card is not a lucky card should be placed in the position of the N card, given the ordered n card, ask how many cards may be lucky cards.
Thinking: Depending on how many cards and whether there are special cards and where special cards are classified, the code is more complex to write, and some cases can be merged.
#include <iostream> #include <cstdio> #include <algorithm> const int MAXN = 100005;
struct node{char c[2];
int x;
}NO[MAXN];
int n,m;
Give each card number, from 1 to 3*m int cal (struct Node A) {if (a.c[0] = = ' C ') {return a.x;
else if (a.c[0] = = ' B ') {return m + a.x;
else{return 2 * m + a.x;
int main (void) {int t;
scanf ("%d", &t);
while (t--) {scanf ("%d%d", &n,&m);
int pos = 0;
for (int i = 1; I <= n; i++) {scanf ("%s", no[i].c);
if (no[i].c[0]!= ' W ') {scanf ("%d", &no[i].x);
} else{pos = i;
} int ans = 0;
When there are only 1 cards, whatever this card is, all cards are likely to be lucky cards if (n = = 1) {ans = 3 * m; ///There are two cards, else if (n = = 2) {//No w if (pos = 0) {//If the number of the first card is greater than the second, only the first card may be a lucky card if (Cal (No[1]) > Cal (n
O[2]) {ans = 1;
else{//Otherwise the other cards not appearing in these n cards and the first card of the N card may be the lucky card ans = 3 * M-(n-1);
}//w is in 1 position, all cards numbered less than the second card may be lucky card else if (pos = 1) {ans = cal (no[2])-1; //W in 2 position, first cardAnd all cards with a number greater than the first card are likely to be lucky cards else{ans = 3 * m-cal (no[1]) + 1;
}///Three and more than three cards else{//ditto if (pos = 0) {if (Cal (no[1)) > Cal (no[2)) {ans = 1;
else{ans = 3 * M-(n-1);
}//Ibid. else if (pos = 1) {ans = cal (no[2])-1;
//W at 2, the first card and all cards with a number greater than the first card less than the third card can be lucky card else if (pos = 2) {ans = cal (no[3])-Cal (No[1));
//Ibid. Else if (pos = n) {if (Cal (no[1)) > Cal (no[2)) {ans = 1;
else{ans = 3 * m-cal (no[n]);
}//Ibid. else{if (Cal (No[1]) > Cal (no[2)) {ans = 1;
else{//w in 3 to n-1 position, all cards with number greater than pos-1 card less than POS + 1 cards may be the lucky card ans = cal (No[pos + 1])-Cal (No[pos-1)-1;
} printf ("%d\n", ans);
return 0; }
Author:chen, Shihan
Source:the 15th Zhejiang Provincial Collegiate Programming Contest sponsored by Tusimple