CF1473C.No More Inversions

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a sequence aa with nn elements 1,2,3,…,k−1,k,k−1,k−2,…,k−(n−k)1, 2, 3, \dots, k - 1, k, k - 1, k - 2, \dots, k - (n - k) (k≤n<2kk \le n \lt 2k).

Let's call as inversion in aa a pair of indices i<ji \lt j such that a[i]>a[j]a[i] \gt a[j].

Suppose, you have some permutation pp of size kk and you build a sequence bb of size nn in the following manner: b[i]=p[a[i]]b[i] = p[a[i]].

Your goal is to find such permutation pp that the total number of inversions in bb doesn't exceed the total number of inversions in aa, and bb is lexicographically maximum.

Small reminder: the sequence of kk integers is called a permutation if it contains all integers from 11 to kk exactly once.

Another small reminder: a sequence ss is lexicographically smaller than another sequence tt, if either ss is a prefix of tt, or for the first ii such that si≠tis_i \ne t_i, si<tis_i \lt t_i holds (in the first position that these sequences are different, ss has smaller number than tt).

你有一个长度为 nn 的序列 aa,其元素为 1,2,3,…,k−1,k,k−1,k−2,…,k−(n−k)1, 2, 3, \dots, k - 1, k, k - 1, k - 2, \dots, k - (n - k)(其中 k≤n<2kk \le n < 2k)。

我们称 aa 中的一个逆序对为满足 i<ji < j 且 a[i]>a[j]a[i] > a[j] 的下标对 (i,j)(i, j)。

假设你有一个大小为 kk 的排列 pp,并按如下方式构造一个长度为 nn 的序列 bb:b[i]=p[a[i]]b[i] = p[a[i]]。

你的目标是找出一个排列 pp,使得 bb 中的逆序对总数不超过 aa 中的逆序对总数,且 bb 在字典序上尽可能大。

小提示:若一个包含 kk 个整数的序列恰好包含从 11 到 kk 的每个整数一次,则称其为一个排列。

另一个小提示:若序列 ss 比序列 tt 字典序更小,则满足以下条件之一:ss 是 tt 的前缀;或存在第一个下标 ii 使得 si≠tis_i \ne t_i,且 si<tis_i < t_i(即在两序列首次出现差异的位置上,ss 的对应元素小于 tt 的对应元素)。

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first and only line of each test case contains two integers nn and kk (k≤n<2kk \le n \lt 2k; 1≤k≤1051 \le k \le 10^5) — the length of the sequence aa and its maximum.

It's guaranteed that the total sum of kk over test cases doesn't exceed 10510^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行(也是唯一一行)包含两个整数 nn 和 kk(k≤n<2kk \le n \lt 2k;1≤k≤1051 \le k \le 10^5)—— 序列 aa 的长度及其最大值。

保证所有测试用例中 kk 的总和不超过 10510^5。

输出格式

For each test case, print kk integers — the permutation pp which maximizes bb lexicographically without increasing the total number of inversions.

It can be proven that pp exists and is unique.

对于每个测试用例,输出 kk 个整数——即在不增加总逆序对数的前提下,使得序列 bb 的字典序最大的排列 pp。

可以证明,这样的排列 pp 存在且唯一。

输入输出样例

  • 输入#1

    4
    1 1
    2 2
    3 2
    4 3

    输出#1

    1 
    1 2 
    2 1 
    1 3 2

说明/提示

In the first test case, the sequence a=[1]a = [1], there is only one permutation p=[1]p = [1].

In the second test case, the sequence a=[1,2]a = [1, 2]. There is no inversion in aa, so there is only one permutation p=[1,2]p = [1, 2] which doesn't increase the number of inversions.

In the third test case, a=[1,2,1]a = [1, 2, 1] and has 11 inversion. If we use p=[2,1]p = [2, 1], then b=[p[a[1]],p[a[2]],p[a[3]]]=[2,1,2]b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2] and also has 11 inversion.

In the fourth test case, a=[1,2,3,2]a = [1, 2, 3, 2], and since p=[1,3,2]p = [1, 3, 2] then b=[1,3,2,3]b = [1, 3, 2, 3]. Both aa and bb have 11 inversion and bb is the lexicographically maximum.

在第一个测试用例中,序列 a=[1]a = [1],仅存在一个排列 p=[1]p = [1]。

在第二个测试用例中,序列 a=[1,2]a = [1, 2]。aa 中不存在逆序对,因此仅存在一个排列 p=[1,2]p = [1, 2],它不会使逆序对数量增加。

在第三个测试用例中,a=[1,2,1]a = [1, 2, 1],包含 11 个逆序对。若取 p=[2,1]p = [2, 1],则 b=[p[a[1]],p[a[2]],p[a[3]]]=[2,1,2]b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2],同样包含 11 个逆序对。

在第四个测试用例中,a=[1,2,3,2]a = [1, 2, 3, 2],取 p=[1,3,2]p = [1, 3, 2],则 b=[1,3,2,3]b = [1, 3, 2, 3]。aa 和 bb 均恰好有 11 个逆序对,且 bb 是字典序最大的序列。

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

首页