浙江省赛 Mahjong Sorting(分类讨论)

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 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 ,

    then  must be 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 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.

题意:给出3*m张牌,另外有一张特殊的牌,先从3*m张牌中挑选出一张幸运牌,然后在3*m + 1中张牌中挑选n张牌,如果这张幸运牌在这n张牌中,就把他放到最左边,剩下的牌按照题目要求排序,如果那一张特殊的牌也存在于这n张牌中,就把这张牌放在幸运牌为非幸运牌时应该放在这n张牌的位置,给出已经排好序的n张牌,问有多少张牌可能为幸运牌?

思路:根据有多少张牌和是否有特殊的牌以及特殊的牌在哪个位置进行分类讨论,代码写的较复杂,有些情况可以合并。

#include <iostream>
#include <cstdio>
#include <algorithm>

const int maxn = 100005;
struct node{
	char c[2];
	int x;
}no[maxn];
int n,m;

//给每一张牌编号,从1 到  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;
		//只有1张牌的时候,无论这张牌是什么,所有的牌都有可能为幸运牌 
		if(n == 1){
			ans = 3 * m;
		}
		//有两张牌时, 
		else if(n == 2){
			//没有W
			if(pos == 0){
				//如果第一张牌的编号大于第二张,只有第一张牌可能为幸运牌 
				if(cal(no[1]) > cal(no[2])){
					ans = 1;
				}
				else{
					//否则没出现在这N张牌里的其他牌和这N张牌的第一张牌可能为幸运牌 
					ans = 3 * m - (n - 1);
				}
			}
			//W在1位置,编号小于第二张牌的所有牌都可能为幸运牌 
			else if(pos == 1){
				ans = cal(no[2]) - 1;
			}
			//W在2位置,第一张牌以及编号大于第一张牌的所有牌都可能为幸运牌 
			else{
				ans = 3 * m - cal(no[1]) + 1;
			}
		}
		//有三张及三张以上的牌时 
		else{
			//同上 
			if(pos == 0){
				if(cal(no[1]) > cal(no[2])){
					ans = 1;
				}
				else{
					ans = 3 * m - (n - 1);
				}
			}
			//同上 
			else if(pos == 1){
				ans = cal(no[2]) - 1;
			}
			//W在2位置,第一张牌以及编号大于第一张牌小于第三章牌的所有牌都可能为幸运牌 
			else if(pos == 2){
				ans = cal(no[3]) - cal(no[1]);
			}
			//同上 
			else if(pos ==  n){
				if(cal(no[1]) > cal(no[2])){
					ans = 1;
				}
				else{
					ans = 3 * m - cal(no[n]);
				}
			}
			//同上 
			else{
				if(cal(no[1]) > cal(no[2])){
					ans = 1;
				}
				else{
					//W在3到n - 1位置,编号大于pos - 1牌小于pos + 1牌的所有牌都可能为幸运牌 
					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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值