CF2201B.Recollect Numbers

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are 2n2n cards with numbers 1,1,2,2,…,n,n1,1,2,2,\ldots,n,n written on them. In other words, for all j=1,2,…,nj=1,2,\ldots,n, there are exactly 22 cards with the number jj. Each card only has one number written on its front.

You will play a card flipping game. Initially, all 2n2n cards are placed on their back (the side without numbers). In each turn, you flip exactly two cards. If the two cards have the same number, you discard the two cards. Otherwise, you flip them back to their original position. You win when all 2n2n cards are discarded. Note that you do not have to flip the two cards simultaneously, so you can decide on your choice of the second card after seeing the number on the first one.

Consider the following "greedy" algorithm to play the game. Initially, the 2n2n cards are placed in a row arbitrarily. Then your strategy on each turn is as follows:

  • If there are two cards that you have flipped previously and have the same number, flip those two cards.
  • Otherwise, flip the first card*^\text{*} that you have never flipped so far as the first one. Let's say this card has the number xx.
    • Afterwards, if there is another card that you have flipped previously and has the number xx, flip that card.
    • Otherwise, flip the first card∗^{\text{∗}} that you have never flipped so far (including in this turn) as the second one.

It can be shown that the algorithm's strategy is uniquely determined on every turn.

You must solve the following problem regarding the algorithm stated above.

  • Given nn and kk, please find an orientation of the 2n2n cards for which the algorithm above takes exactly kk turns to win the game.

Additionally, if such an orientation does not exist, please report so.

∗^{\text{∗}}Here, "the first card" of some condition refers to the earliest card in the row satisfying that condition.

有 2n2n 张卡片,上面分别写有数字 1,1,2,2,…,n,n1,1,2,2,\ldots,n,n。换言之,对每个 j=1,2,…,nj=1,2,\ldots,n,恰好有两张卡片标有数字 jj。每张卡片的正面仅写有一个数字。

你将进行一场翻牌游戏。初始时,全部 2n2n 张卡片均背面朝上(即无数字的一面朝上)。在每一回合中,你必须恰好翻开两张卡片。若这两张卡片上的数字相同,则将这两张卡片移出游戏;否则,将它们重新翻回背面朝上。当全部 2n2n 张卡片均被移出时,你获胜。注意:你无需同时翻开两张卡片,因此可在看到第一张卡片上的数字后再决定第二张卡片的选择。

考虑以下用于进行该游戏的“贪心”算法。初始时,2n2n 张卡片以某种任意顺序排成一行。随后,你在每一回合中按如下策略操作:

  • 若存在两张你此前已翻开过、且数字相同的卡片,则翻开这两张卡片;
  • 否则,将你此前从未翻开过的、最靠左(即行中位置最前)的卡片作为第一张翻开的卡片*^\text{*}。设该卡片上的数字为 xx。
    • 随后,若存在另一张你此前已翻开过、且数字也为 xx 的卡片,则翻开该卡片;
    • 否则,将你此前从未翻开过(包括本回合内)的、最靠左的卡片作为第二张翻开的卡片∗^{\text{∗}}。

可以证明:该算法在每一回合中的策略都是唯一确定的。

你需要解决关于上述算法的如下问题:

  • 给定 nn 和 kk,请构造一种 2n2n 张卡片的排列方式(即一种线性顺序),使得上述算法恰好经过 kk 回合后获胜。

此外,若不存在满足条件的排列方式,请予以说明。

∗^{\text{∗}}此处,“满足某条件的最靠左的卡片”指在该行中位置最前(即索引最小)且满足该条件的卡片。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1031 \le t \le 10^3). The description of the test cases follows.

The only line of each test case contains two integers nn and kk (1≤n≤300 0001 \le n \le 300\,000, 1≤k≤1 000 0001 \le k \le 1\,000\,000).

It is guaranteed that the sum of nn over all test cases does not exceed 300 000300\,000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1031 \le t \le 10^3)。随后是各测试用例的描述。

每个测试用例仅有一行,包含两个整数 nn 和 kk(1≤n≤300 0001 \le n \le 300\,000,1≤k≤1 000 0001 \le k \le 1\,000\,000)。

保证所有测试用例的 nn 之和不超过 300 000300\,000。

输出格式

If there is an orientation of cards that satisfies the conditions, output "YES" on a new line.

Then, output 2n2n integers a1,a2,…,a2n−1,a2na_1,a_2,\ldots,a_{2n-1},a_{2n} on the next line. Here, aia_i is the number written on the ii-th card.

Do note that the sequence aa must satisfy the following conditions:

  • For each 1≤i≤2n1 \le i \le 2n, 1≤ai≤n1 \le a_i \le n;
  • For each 1≤j≤n1 \le j \le n, jj appears in aa exactly twice;
  • When the cards are placed in this order, the algorithm stated above takes exactly kk turns to win the game.

If there are multiple solutions, print any of them.

If there is no orientation of cards that satisfies the conditions, output "NO" on a separate line.

You can output the answer in any case. For example, the strings "yEs", "yes", and "Yes" will also be recognized as positive responses.

如果存在一种满足条件的卡片朝向,则在新的一行输出“YES”。

然后,在下一行输出 2n2n 个整数 a1,a2,…,a2n−1,a2na_1,a_2,\ldots,a_{2n-1},a_{2n}。其中,aia_i 表示第 ii 张卡片上所写的数字。

请注意,序列 aa 必须满足以下条件:

  • 对每个 1≤i≤2n1 \le i \le 2n,有 1≤ai≤n1 \le a_i \le n;
  • 对每个 1≤j≤n1 \le j \le n,数字 jj 在 aa 中恰好出现两次;
  • 当卡片按此顺序排列时,上述算法恰好需要 kk 轮才能赢得游戏。

若存在多个解,输出任意一个即可。

如果不存在满足条件的卡片朝向,则在单独一行输出“NO”。

你可以以任意大小写形式输出答案。例如,字符串 “yEs”、“yes” 和 “Yes” 均会被识别为肯定回答。

输入输出样例

  • 输入#1

    6
    2 3
    3 4
    3 2
    3 5
    6 10
    6 67

    输出#1

    YES
    2 1 2 1
    YES
    1 3 2 2 1 3
    NO
    YES
    1 2 3 1 2 3
    YES
    2 1 3 4 5 4 1 2 6 5 6 3
    NO

说明/提示

For the first test case, the choices on each turn are as follows:

  1. [2,1,2,1][\color{red}{2},\color{red}{1},2,1]: The two cards have different numbers, so they are flipped back into position.
  2. [2,1,2,1][\color{red}{2},\color{blue}{1},\color{red}{2},1]: The two cards have the same number, so they get discarded.
  3. [1,1][\color{red}{1},\color{red}{1}]: The two cards have the same number, so they get discarded.

Here, the red numbers indicate the cards flipped on the current turn, and the blue numbers indicate cards that you have flipped previously.

For the fourth test case, the choices on each turn are as follows:

  1. [1,2,3,1,2,3][\color{red}{1},\color{red}{2},3,1,2,3]: The two cards have different numbers, so they are flipped back into position.
  2. [1,2,3,1,2,3][\color{blue}{1},\color{blue}{2},\color{red}{3},\color{red}{1},2,3]: The two cards have different numbers, so they are flipped back into position.
  3. [1,2,3,1,2,3][\color{red}{1},\color{blue}{2},\color{blue}{3},\color{red}{1},2,3]: The two cards have the same number, so they get discarded.
  4. [2,3,2,3][\color{red}{2},\color{blue}{3},\color{red}{2},3]: The two cards have the same number, so they get discarded.
  5. [3,3][\color{red}{3},\color{red}{3}]: The two cards have the same number, so they get discarded.

The algorithm took exactly k=5k=5 turns to win the game.

对于第一个测试用例,每一轮的选择如下:

  1. [2,1,2,1][\color{red}{2},\color{red}{1},2,1]:两张牌的数字不同,因此将它们翻回原位。
  2. [2,1,2,1][\color{red}{2},\color{blue}{1},\color{red}{2},1]:两张牌的数字相同,因此将它们移除。
  3. [1,1][\color{red}{1},\color{red}{1}]:两张牌的数字相同,因此将它们移除。

此处,红色数字表示当前轮次翻起的牌,蓝色数字表示你之前已翻起过的牌。

对于第四个测试用例,每一轮的选择如下:

  1. [1,2,3,1,2,3][\color{red}{1},\color{red}{2},3,1,2,3]:两张牌的数字不同,因此将它们翻回原位。
  2. [1,2,3,1,2,3][\color{blue}{1},\color{blue}{2},\color{red}{3},\color{red}{1},2,3]:两张牌的数字不同,因此将它们翻回原位。
  3. [1,2,3,1,2,3][\color{red}{1},\color{blue}{2},\color{blue}{3},\color{red}{1},2,3]:两张牌的数字相同,因此将它们移除。
  4. [2,3,2,3][\color{red}{2},\color{blue}{3},\color{red}{2},3]:两张牌的数字相同,因此将它们移除。
  5. [3,3][\color{red}{3},\color{red}{3}]:两张牌的数字相同,因此将它们移除。

该算法恰好用了 k=5k=5 轮赢得游戏。

输入解题思路,AI测评打分。不知道怎么写?

首页