Mahjong Sorting ZOJ - 4034

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.


题意: 

在3m+1张牌中选n张牌,但在选之前先在3m张牌(除白板外)里挑选一张牌规定它为“幸运牌”

如果n张牌里包含这张“幸运牌”,那么这张“幸运牌”一定是在最左边的位置(第一张);

如果n张牌里包含白板,那么这张白板相当于“不幸运”版本的“幸运牌”,(也就是只是和“幸运牌”的点数和种类一样,没有"幸运"这个性质。)比如题中给的例子,“三万”作为”幸运牌“,白板就是“不幸版本”的“三万”,所以放在“二万”和“四万”中间。

排放是按照相同种类的牌点数小的在点数大的牌的左边,种类是从左到右依次是“万”, “条”, “筒”。即(一万、两万、...九万、一条、两条、...九条、一筒、两筒...九筒的顺序)。

输入你已经挑选出来的n张牌,问可能的幸运牌的种类是多少。

 

思路:(1)幸运牌”如果确定在这n张牌里那么一定是在n张牌的最左边。(2)如果不确定在不在,那么n张牌里的白板会相当于(不幸版本)的“幸运牌”,需要放在合适的位置(题中所给的顺序)。如果没有白板,幸运牌只能是所选的n张牌里的第一张牌(在)和n张牌之外的牌(不在),也就是3*m-(n-1)。

 

1、当n=1时,我们无法确定这张牌是不是幸运牌,即有可能这张是,有可能这张不是(那么幸运牌就是没有被选到的之一),所以一共3*m种。

2、当n>1张牌时:

①:当n>=2时,当第一张牌比第二张牌的点数大并且第二张牌不是白板时(说明第一张牌没有按从大到小顺序排放),结果为1(确定第一张牌为幸运牌)。

②:当n张牌里没有白板时,结果为3*m-(n-1)。(在通过摆放顺序看不出来是否存在幸运牌就有两种可能 1:第一张牌就是(在),2:在没有被选中的牌中之一(不在))。

③:当有白板时:

Ⅰ:当白板是第一张牌时,结果为第二张牌的点数-1。

Ⅱ:当白板是第二张牌时,结果为第三张牌的点数-第一张牌的点数。(有幸运牌: 第一张(与幸运牌一样点数的白板放在幸运牌之后);没有幸运牌:第三张的点数-第一张的点数-1)。

Ⅲ:当白板在其余位置时,结果为白板后一张牌的点数 - 白板前一张牌的点数 -1.

 


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
const int maxn = 100010;
int mapp[maxn];
int main()
{
    int t, m, n, k, p;
    scanf("%d", &t);
    while(t--)
    {
        char str[3];
        scanf("%d%d", &n, &m);//选择n张,每种m张;
        p = -1;
        mapp[n] = 3*m+1;
        for(int i=0; i<n; i++)
        {
            scanf("%s", str);

            //如果是白板,标记第几张牌是白板;
            if(str[0]=='W')
            {
                p = i;
                continue;
            }
            scanf("%d", &k);

            //如果是“万”,他就应该在万顺序的第k个,即所有位置的第k个;
            if(str[0]=='C')
            {
                mapp[i] = k;
            }

            //如果是“条”,他就应该在条顺序的第k个,即所有顺序的第m+k个;
            else if(str[0]=='B')
            {
                mapp[i] = m+k;
            }

            //如果是“筒”,他就应该在筒顺序的第k个,即所有顺序的第2*m+k个;
            else
                mapp[i] = 2*m+k;
        }

        //如果被选中的牌只有一张,就有3*m个可能;
        if(n==1)
            printf("%d\n",3*m);

        //如果第一张牌比第二张牌大并且第二张牌不是白板;
        else if(mapp[0]>mapp[1]&&p!=1)
        {
            printf("1\n");
        }
        //如果没有白板;
        else if(p==-1)
        {
            printf("%d\n", 3*m-n+1);
        }
        //如果有白板;
        else
        {
            if(p==0)//如果有白板,并且是第一张;
                printf("%d\n", mapp[1]);
            else if(p==1)//如果有白板是第二张;
                printf("%d\n", mapp[2]-mapp[0]);
            else//如果有白板在其他位置;
                printf("%d\n", mapp[p+1]-mapp[p-1]-1);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值