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

    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.

思维题:

要分有W和没有W两大类分别进行讨论,

当没有W时,    1.如果序列不是从大到小的,既第一个大于第二个的(在n大于1的时候)时候maxn = 1

                      2.如果序列是有序的,那么maxn = 3*m-n+1,可以验证对n为1的情况也适用

当有W时

                    1.当posw(就是W的位置)=1&& n=1时,maxn=3*m

                   2.当posw=1时,maxn = jian('C', 1, th[2].lei, th[2].rank) + 1;jian函数求的是两个数之间的数的个数,不包含边界,所以要加上1

                    3.当posw=2&&n==2时maxn = 2 + jian(th[1].lei, th[1].rank, 'D', m);

                     4.当posw=2&&n>=3时maxn = jian(th[1].lei, th[1].rank, th[3].lei, th[3].rank) + 1;

                  5.当posw=n时如果第一个数大于第二个数那么maxn=1,否则maxn = jian(th[n-1].lei, th[n-1].rank, 'D', m) + 1;

                  6.当posw在中间但不为2时,如果第一个数大于第二个数那么maxn=1,否则maxn = jian(th[posw-1].lei,th[posw-1].rank,th[posw+1].lei, th[posw+1].rank);


#include <bits/stdc++.h>

using namespace std;

int n, m;
struct node {
char lei;
int rank;
}th[100010];

int jian(char lei1, int rank1, char lei2, int rank2)
{
    if(lei1 == lei2){
        return rank2 - rank1 - 1;
    }else {
        if(lei1 == 'C'){
            if(lei2 == 'B') return m - rank1 + rank2 - 1;
            else if(lei2 == 'D') return m + m - rank1 + rank2 - 1;
        }else if(lei1 == 'B'){
            return rank2 - 1 + m - rank1;
        }
    }
}
bool fou(int pos)
{
        if(th[pos].lei == th[pos+1].lei){
            if(th[pos].rank < th[pos+1].rank) return true;
            else return false;
        }else {
            if(th[pos].lei == 'C' && th[pos+1].lei=='B') return true;
            else if((th[pos].lei == 'B' || th[pos].lei == 'C')&& th[pos+1].lei=='D') return true;
            return false;
        }
}
int main()
{
    int num;
    scanf("%d", &num);
    while(num --){
        int posw = -1;
        int maxn = 0;
        scanf("%d %d", &n, &m);
        getchar();
        for(int i = 1; i <= n; i ++){
            scanf("%c", &th[i].lei);
            if(th[i].lei != 'W'){
                scanf("%d", &th[i].rank);
            }else {
                posw = i;
            }
            getchar();
        }
        if(posw == -1){
            if(n > 1 && !fou(1)) maxn = 1;
            else maxn = 3 * m - n + 1;
        }else {
            if(n == 1) maxn = 3 * m;
            else if(posw == 1){
                maxn = jian('C', 1, th[2].lei, th[2].rank) + 1;
            }else if(posw == 2){
                if(n == 2) maxn = 2 + jian(th[1].lei, th[1].rank, 'D', m);
                else {
                    maxn = jian(th[1].lei, th[1].rank, th[3].lei, th[3].rank) + 1;
                }
            }else if(posw == n){
                if(!fou(1)) maxn = 1;
                else maxn = jian(th[n-1].lei, th[n-1].rank, 'D', m) + 1;
            }else {
                if(!fou(1)) maxn = 1;
                else maxn = jian(th[posw-1].lei,th[posw-1].rank,th[posw+1].lei, th[posw+1].rank);
            }
        }
        cout << maxn << endl;
    }
    return 0;
}

             

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值