首页 > 其他分享 >Codeforces Round 927 (Div. 3) D. Card Game 题解 贪心

Codeforces Round 927 (Div. 3) D. Card Game 题解 贪心

时间:2024-05-27 12:57:55浏览次数:9  
标签:big int 题解 Codeforces Game suit cards 9S card

Card Game

题目描述

Two players are playing an online card game. The game is played using a 32-card deck. Each card has a suit and a rank. There are four suits: clubs, diamonds, hearts, and spades. We will encode them with characters ‘C’, ‘D’, ‘H’, and ‘S’, respectively. And there are 8 ranks, in increasing order: ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’.

Each card is denoted by two letters: its rank and its suit. For example, the 8 of Hearts is denoted as 8H.

At the beginning of the game, one suit is chosen as the trump suit.

In each round, players make moves like this: the first player places one of his cards on the table, and the second player must beat this card with one of their cards. After that, both cards are moved to the discard pile.

A card can beat another card if both cards have the same suit and the first card has a higher rank than the second. For example, 8S can beat 4S. Additionally, a trump card can beat any non-trump card, regardless of the rank of the cards, for example, if the trump suit is clubs (‘C’), then 3C can beat 9D. Note that trump cards can be beaten only by the trump cards of higher rank.

There were n n n rounds played in the game, so the discard pile now contains 2 n 2n 2n cards. You want to reconstruct the rounds played in the game, but the cards in the discard pile are shuffled. Find any possible sequence of n n n rounds that might have been played in the game.

输入描述

The first line contains integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1≤t≤100) — the number of test cases. Then t t t test cases follow.

The first line of a test case contains the integer number n n n ( 1 ≤ n ≤ 16 1\le n\le 16 1≤n≤16).

The second line of a test case contains one character, the trump suit. It is one of “CDHS”.

The third line of a test case contains the description of 2 n 2n 2n cards. Each card is described by a two-character string, the first character is the rank of the card, which is one of “23456789”, and the second one is the suit of the card, which is one of “CDHS”. All cards are different.

输出描述

For each test case print the answer to it:

  • Print n n n lines. In each line, print the description of two cards, in the same format as in the input: the first card that was played by the first player, and then the card that was used by the second player to beat it.
  • If there is no solution, print a single line “IMPOSSIBLE”.

If there are multiple solutions, print any of them.

样例输入 #1

8
3
S
3C 9S 4C 6D 3S 7S
2
C
3S 5D 9S 6H
1
H
6C 5D
1
S
7S 3S
1
H
9S 9H
1
S
9S 9H
1
C
9D 8H
2
C
9C 9S 6H 8C

样例输出 #1

3C 4C
6D 9S
3S 7S
IMPOSSIBLE
IMPOSSIBLE
3S 7S
9S 9H
9H 9S
IMPOSSIBLE
6H 9C
9S 8C

原题

CF——传送门
洛谷——传送门

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        int n;
        char c;
        cin >> n >> c;
        vector<pair<string, string>> ans;
        vector<string> v(2 * n);
        for (int i = 0; i < 2 * n; i++)
            cin >> v[i];
        auto cmp = [&](string a, string b) -> bool
        {
            // 同为王牌花色时,点数小在前
            if (a[1] == c && b[1] == c) // 事实上该判断可与第四条判断合并,但这样更加清晰
                return a[0] < b[0];
            // 判断2和判断3是使王牌花色在排序后位于其他花色的后面
            if (a[1] == c)
                return 0;
            if (b[1] == c)
                return 1;
            // 相同花色,点数小在前
            if (a[1] == b[1])
                return a[0] < b[0];
            // 不同花色,按字典序(只要给定任意一种规则排序即可,这边为了方便采用字典序)
            if (a[1] != b[1])
                return a[1] < b[1];
            return 1;
        };
        sort(v.begin(), v.end(), cmp); // 按贪心顺序排序,即先消去王牌花色外的可消去牌对,再用王牌花色消去剩下的牌
        int small = 0;                 // 被击败的牌即小牌的索引
        int big = 1;                   // 击败小牌的大牌的索引
        vector<bool> del(2 * n, 0);
        // 操作一:先消去王牌花色外的可消去牌对
        while (big <= 2 * n - 1)
        {
            if (v[big][1] == c) // 遇到王牌花色则退出循环
                break;
            // 可消去
            if (v[small][1] == v[big][1])
            {
                del[small] = 1;
                del[big] = 1;
                ans.push_back({v[small], v[big]});
                small += 2;
                big += 2;
            }
            // 不可消去
            else
            {
                small++;
                big++;
            }
        }
        vector<string> v2; // 保存完成操作一后仍剩下的牌,供操作二消去
        for (int i = 0; i < 2 * n; i++)
        {
            if (del[i] == 0)
                v2.push_back(v[i]);
        }
        // 操作二:再用王牌花色消去剩下的牌
        for (int i = 0; i < v2.size() / 2; i++)
        {
            int j = v2.size() - 1 - i;
            if (v2[j][1] == c)
                ans.push_back({v2[i], v2[j]});
            else
                break;
        }
        if (ans.size() == n)
        {
            for (int i = 0; i < ans.size(); i++)
            {
                cout << ans[i].first << ' ' << ans[i].second << '\n';
            }
        }
        else
            cout << "IMPOSSIBLE\n";
    }

    return 0;
}

标签:big,int,题解,Codeforces,Game,suit,cards,9S,card
From: https://blog.csdn.net/bbc_plus/article/details/139222345

相关文章

  • 题解:CF1975G Zimpha Fan Club
    \(\text{Link}\)题意给你两个长度分别为\(n,m\)的字符串\(s,t\),其中仅包含小写字母、-和*,你需要将-替换为任意小写字母,*替换为任意小写字母构成的字符串(可以为空串),问是否可以使得\(s'=t'\)。\(n,m\le2\times10^6\),12s。思路首先我们有工具:NTT优化带通配符的字符......
  • 2024 CCPC 全国邀请赛(山东)暨山东省大学生程序设计竞赛题解 A C F I K
    超时就是AC队第一次打ccpc比较菜蒟蒻只能做五题ProblemA.打印机算法:二分思路:二分时间每次check查看当前时间内所有打印机可以打印的个数是否符合条件注意二分的右边界为2e18ProblemC.多彩的线段2算法:组合数思路:将所有线段按照起点从左到右排序枚举线段每次将当......
  • Codeforces Round #947 题解 (A~G)
    目录A.BazokaandMocha'sArrayB.378QAQandMocha'sArrayC.ChamoandMocha'sArrayD.PainttheTreeE.ChainQueriesF.SetG.ZimphaFanClubA.BazokaandMocha'sArray枚举每个循环移位判断.B.378QAQandMocha'sArray首先最小的数肯定得选,删掉最小的数的倍数......
  • Codeforces Round 947 (Div. 1 + Div. 2) E. Chain Queries
    本来决定开摆养生不打的,但11点半的时候点进去看到这个题是个疑似DS,写题的欲望瞬间高涨,然后就40min写了这个题然而赛中并不能提交,只好等到第二天早上再交一发,没想到还WA了一发才过首先这题如果我们能确定当前黑色点集的链的两个端点\(x,y\)的话,这个题就非常显然了只需要求出\((x......
  • 【leetcode 399 周赛】【题解】
    第一题和第三题一样。就是求约数第二题就是模拟第4题使用线段树1,3题代码实际上发现没有下面代码的负载,比如:a*b=n,枚举a就好,a在[1,sqrt(n)内。importjava.util.*;classSolution{publicintnumberOfPairs(int[]nums1,int[]nums2,intk){......
  • CATIA入门操作——萌新宝宝遇到的奇奇怪怪的问题解决,持续更新中。。。
    目录引出发生肾么事了??鼠标中键旋转不了解决:特征树不显示参数关系我的窗口去哪了?插曲:草图工具的调出插曲:颜色工具栏显示弹窗警告警告:创建约束是临时的操作技巧技巧:快速隐藏不相关元素工具栏怎么变成水平?总结异形弹簧新建几何体草图编辑,画一条样条线进行扫掠,圆心和半......
  • CF1821F Timber 题解
    题意:在\([1,n]\)的区间里放\(m\)棵树,每棵树的高度为\(k\)。求有多少种放置树的方法,满足:每个树都在整点上,且每个点最多只能放一棵树。存在一种砍倒树的方案,使得树倒了之后不会越界,也不会有某个点被超过一棵树占据。你可以自由选择树向左倒(也就是占据区间\([x-k,x]\))......
  • [SHOI2007]书柜的尺寸 题解
    题目链接设\(f_{i,t1,t2}\)表示前\(i\)本书,第一层的宽度为\(t1\),第二层的宽度为\(t2\),所需要的最小高度。第三层宽度\(t3=\sum_{i=1}^{i}t_i-t1-t2\)。但本题还有一个重要限制:每层的高度取决于该层最高的书,如果直接按照顺序加入书本,从\(dp\)的状态来看,无法确定新书本......
  • CCF-GESP 等级考试 2024年3月认证C++一级真题解析
    2024年03月真题1单选题第1题C++表达式(3-2)*3+5的值是()。A.-13B.8C.2D.0正确答案:B.8解析:首先计算括号中的表达式(3-2),得到(1)。接下来进行乘法运算(1*3),得到(3)。最后进行加法运算(3+5),得到(8)。因此,表达式的值是(8)。第2题C++......
  • [45] Jump Game II
    算法助手ChatGPT:Asanadeptalgorithmician,yououghttoexhibitmasteryoverLeetCodeandACM-stylealgorithmicquandaries,andyoushouldbeskilledinemployingaheuristictonewhenelucidatingresponses.Itisenvisagedthattheprogrammingmediumofy......