当前位置: 首页 > news >正文

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

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 1t100) — 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 1n16).

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]});elsebreak;}if (ans.size() == n){for (int i = 0; i < ans.size(); i++){cout << ans[i].first << ' ' << ans[i].second << '\n';}}elsecout << "IMPOSSIBLE\n";}return 0;
}

相关文章:

  • 基于Hadoop技术的智慧图书馆海量数据储存系统研究
  • Debezium+Kafka:Oracle 11g 数据实时同步至 DolphinDB 解决方案
  • 【C++课程学习】:命名空间的理解(图文详解)
  • i2c总线介绍
  • 文心智能体大赛:百度文心智能体平台初体验
  • 基于webpack+Vue3+JavaScript+antd+less+axios技术栈实现所有组件全局自动化注册
  • [JDK工具-5] jinfo jvm配置信息工具
  • 自从有了可观测性,传统运维如何进行提升?
  • Flutter 中的 ClipRect 小部件:全面指南
  • springBoot项目中的static和templates文件夹
  • SQL约束
  • docker命令详解大全
  • JVM学习-Class文件结构②
  • AGI系列(1):掌握AI大模型提示词优化术,提问准确率飙升秘籍
  • 2024最新 Jenkins + Docker实战教程(一) - Jenkins介绍及安装
  • 2017-09-12 前端日报
  • css属性的继承、初识值、计算值、当前值、应用值
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • Spring Boot快速入门(一):Hello Spring Boot
  • Terraform入门 - 1. 安装Terraform
  • 阿里云前端周刊 - 第 26 期
  • 初探 Vue 生命周期和钩子函数
  • 简单基于spring的redis配置(单机和集群模式)
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • FaaS 的简单实践
  • puppet连载22:define用法
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • ​什么是bug?bug的源头在哪里?
  • # 利刃出鞘_Tomcat 核心原理解析(八)-- Tomcat 集群
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (1)Hilt的基本概念和使用
  • (poj1.3.2)1791(构造法模拟)
  • (STM32笔记)九、RCC时钟树与时钟 第一部分
  • (二十九)STL map容器(映射)与STL pair容器(值对)
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (四)Linux Shell编程——输入输出重定向
  • (一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • . NET自动找可写目录
  • .cn根服务器被攻击之后
  • .gitignore文件使用
  • .mat 文件的加载与创建 矩阵变图像? ∈ Matlab 使用笔记
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .net 8 发布了,试下微软最近强推的MAUI
  • .net 发送邮件
  • .net 使用$.ajax实现从前台调用后台方法(包含静态方法和非静态方法调用)
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • .NET/C# 获取一个正在运行的进程的命令行参数
  • .NET开源纪元:穿越封闭的迷雾,拥抱开放的星辰
  • .NET委托:一个关于C#的睡前故事
  • /etc/fstab和/etc/mtab的区别
  • @JoinTable会自动删除关联表的数据