DreamGrid has just found a set of Mahjong with 3M suited tiles and a White Dragon tile in his pocket. Each suited tile has a suit (Character, Bamboo or Dot) and a rank (ranging from 1 to
M), and there is exactly one tile of each 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 first selects one of the 3 M
suited tiles as the “lucky tile”, then he picks Ntiles from the set of (3M+1) tiles and sorts these N
tiles with the following rules:
- The “lucky tile”, if contained in the N tiles, must be placed in the leftmost position.
- For two tiles A and B such that neither of them is the “lucky tile”, if
- A is a Character tile and B is a Bamboo tile, or
- A is a Character tile and B is a Dot tile, or
- A is a Bamboo tile and B is a Dot tile, or
- A and B have the same suit and the rank of A is smaller than the rank of B,
- then A must be placed to the left of B.
White Dragon tile is a special tile. If it’s contained in the N
tiles, it’s considered as the original (not-lucky) version of the lucky tile during the sorting. For example, consider the following sorted tiles, where “3 Character” is selected as the lucky tile. White Dragon tile, in this case, is considered to be the 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 is after the sorting! Given
N sorted tiles, please tell DreamGrid the number of possible lucky tiles.
Input
There are multiple test cases. The first line of the input contains an integer
T, indicating the number of test cases. For each test case:
The first line contains two integers N and M (1≤N,M≤
1
0
6
{10^6}
106, N≤3M+1), indicating the number of sorted tiles and the maximum rank of suited tiles.
For the next N lines, the i-th line describes the i-th sorted tile counting from left to right. The line begins with a capital letter si (si∈{C,B,D,W}), indicating the suit of the i-th tile:
If
s
i
{s_i}
si=C, then an integer r i(1≤
r
i
{r_i}
ri≤M) follows, indicating that it’s a Character tile with rank
r
i
{r_i}
ri;
If
s
i
{s_i}
si=B, then an integer ri (1≤
r
i
{r_i}
ri≤M) follows, indicating that it’s a Bamboo tile with rank
r
i
{r_i}
ri;
If
s
i
{s_i}
si=D, then an integer ri (1≤
r
i
{r_i}
ri≤M) follows, indicating that it’s a Dot tile with rank
r
i
{r_i}
ri;
If
s
i
{s_i}
si=W, then it’s a White Drangon tile.
It’s guaranteed that there exists at least one possible lucky tile, and the sum of N in all test cases doesn’t exceed 1 0 6 {10^6} 106.
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 100
C 2
W
C 9
3 9
C 1
B 2
D 3
Sample Output
2
4
7
25
Hint
For the first sample, “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 tiles.
#include <stdio.h>
#include <iostream>
using namespace std;
const int N = 100100;
int main()
{
int T;
int a[N];
cin >> T;
while (T--)
{
int ind = -1;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
int r;
char c;
cin >> c;
if (c != 'W')
{
cin >> r;
if (c == 'C')
a[i] = r;
else if (c == 'B')
a[i] = m + r;
else if (c == 'D')
a[i] = m * 2 + r;
}
else
{
ind = i;
}
}
if (n == 1)
{
cout << 3 * m << endl;
}
else if (ind == -1)
{
if (a[0] < a[1])
cout << 3 * m - n + 1 << endl;
else
cout << 1 << endl;
}
else if (n == 2)
{
if (ind == 0)
{
cout << a[1] - 1 << endl;
}
else
{
cout << 3 * m - a[0] + 1 << endl;
}
}
else
{
if (ind == 0)
{
cout << a[1] - 1 << endl;
}
else if (ind == 1)
{
cout << a[2] - a[0] << endl;
}
else if (a[0] > a[1])
{
cout << 1 << endl;
}
else if (ind == n - 1)
{
cout << 3 * m - a[ind - 1] << endl;
}
else
{
cout << a[ind + 1] - a[ind - 1] - 1 << endl;
}
}
}
return 0;
}
参考该大佬的博客:大佬博客
总结
- 应该先给每张牌编号再来比较大小。
- N的大小和白板的位置以及开始的两张牌决定这幸运牌的个数,在分情况讨论时都要考虑到,刚开始的时候忘了考虑前两张牌的大小。
- 这题初始化会超时。