题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4034
Mahjong Sorting
DreamGrid has just found a set of Mahjong with 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 ), 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 suited tiles as the "lucky tile", then he picks tiles from the set of tiles and sorts these tiles with the following rules:
-
The "lucky tile", if contained in the tiles, must be placed in the leftmost position.
-
For two tiles and such that 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 ,
-
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 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 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 , indicating the number of test cases. For each test case:
The first line contains two integers and (, ), indicating the number of sorted tiles and the maximum rank of suited tiles.
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 that 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 tile, and the sum of in all test cases doesn't exceed .
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.
题解:
这题太难读了,比赛的时候看了两个小时,比赛结束看题解都不会。
在排序过程中,有白板则将白板的牌面看作是幸运牌牌面
如果幸运牌在n个牌中,幸运牌放最左边
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e5+5; 4 int num[maxn],n,m,pos,ans; 5 int main() 6 { 7 int T; 8 cin>>T; 9 for(int i=1;i<=T;i++) 10 { 11 pos=0; 12 cin>>n>>m; 13 for(int j=1;j<=n;j++) 14 { 15 int b; 16 char k; 17 cin>>k; 18 if(k=='W') 19 { 20 pos=j; 21 } 22 else if(k=='C') 23 { 24 cin>>b; 25 num[j]=b; 26 } 27 else if(k=='B') 28 { 29 cin>>b; 30 num[j]=m+b; 31 } 32 else if(k=='D') 33 { 34 cin>>b; 35 num[j]=m*2+b; 36 } 37 } 38 if(n==1)//一张牌时,无论这张牌是什么,所有牌都可能是幸运牌 39 { 40 cout<<3*m<<endl;; 41 } 42 else if(n==2) 43 { 44 if(pos==1)//当W在1号位置时,比2号牌小的全是幸运牌,不包括2号牌 45 cout<<num[2]-1<<endl; 46 else if(pos==2)//比1号牌大,包括1号牌 47 cout<<3*m-num[1]+1<<endl; 48 else 49 {//没有W 50 if(num[1]>num[2])//乱序了,只有1张幸运牌 51 cout<<1<<endl; 52 else//有序,则除了第二张都是 53 cout<<3*m-1<<endl; 54 } 55 }//三张及以上的幸运牌 56 else 57 { 58 if(pos==1) 59 { 60 cout<<num[2]-1<<endl; 61 } 62 else if(pos==2) 63 { 64 cout<<num[3]-num[1]<<endl; 65 } 66 else if(pos==0) 67 { 68 if(num[1]>num[2]) 69 cout<<1<<endl; 70 else 71 { 72 cout<<3*m-n+1<<endl; 73 } 74 } 75 else 76 { 77 if(num[1]>num[2]) 78 cout<<1<<endl; 79 else 80 { 81 if(pos==n) 82 cout<<3*m-num[pos-1]<<endl; 83 else 84 cout<<num[pos+1]-num[pos-1]-1<<endl; 85 } 86 } 87 } 88 } 89 90 return 0; 91 }