You have a sequence 𝑎 with 𝑛 elements 1,2,3,…,𝑘−1,𝑘,𝑘−1,𝑘−2,…,𝑘−(𝑛−𝑘) (𝑘≤𝑛<2𝑘).
Let’s call as inversion in 𝑎 a pair of indices 𝑖<𝑗 such that 𝑎[𝑖]>𝑎[𝑗].
Suppose, you have some permutation 𝑝 of size 𝑘 and you build a sequence 𝑏 of size 𝑛 in the following manner: 𝑏[𝑖]=𝑝[𝑎[𝑖]].
Your goal is to find such permutation 𝑝 that the total number of inversions in 𝑏 doesn’t exceed the total number of inversions in 𝑎, and 𝑏 is lexicographically maximum.
Small reminder: the sequence of 𝑘 integers is called a permutation if it contains all integers from 1 to 𝑘 exactly once.
Another small reminder: a sequence 𝑠 is lexicographically smaller than another sequence 𝑡, if either 𝑠 is a prefix of 𝑡, or for the first 𝑖 such that 𝑠𝑖≠𝑡𝑖, 𝑠𝑖<𝑡𝑖 holds (in the first position that these sequences are different, 𝑠 has smaller number than 𝑡).
Input
The first line contains a single integer 𝑡 (1≤𝑡≤1000) — the number of test cases.
The first and only line of each test case contains two integers 𝑛 and 𝑘 (𝑘≤𝑛<2𝑘; 1≤𝑘≤105) — the length of the sequence 𝑎 and its maximum.
It’s guaranteed that the total sum of 𝑘 over test cases doesn’t exceed 105.
Output
For each test case, print 𝑘 integers — the permutation 𝑝 which maximizes 𝑏 lexicographically without increasing the total number of inversions.
It can be proven that 𝑝 exists and is unique.
Example
inputCopy
4
1 1
2 2
3 2
4 3
outputCopy
1
1 2
2 1
1 3 2
Note
In the first test case, the sequence 𝑎=[1], there is only one permutation 𝑝=[1].
In the second test case, the sequence 𝑎=[1,2]. There is no inversion in 𝑎, so there is only one permutation 𝑝=[1,2] which doesn’t increase the number of inversions.
In the third test case, 𝑎=[1,2,1] and has 1 inversion. If we use 𝑝=[2,1], then 𝑏=[𝑝[𝑎[1]],𝑝[𝑎[2]],𝑝[𝑎[3]]]=[2,1,2] and also has 1 inversion.
In the fourth test case, 𝑎=[1,2,3,2], and since 𝑝=[1,3,2] then 𝑏=[1,3,2,3]. Both 𝑎 and 𝑏 have 1 inversion and 𝑏 is the lexicographically maximum.
题意:
a
a
a序列
1
,
2
,
3
,
4
,
.
.
k
,
k
−
1
,
k
−
2
,
.
.
.
,
k
−
(
n
−
k
)
1,2,3,4,..k,k-1,k-2,...,k-(n-k)
1,2,3,4,..k,k−1,k−2,...,k−(n−k),
求一个排列
p
p
p,使得
b
[
i
]
=
p
[
a
[
i
]
]
b[i]=p[a[i]]
b[i]=p[a[i]],则
b
b
b序列的逆序数不大于
a
a
a序列,且
b
b
b的字典序最大。
思路:
可以发现,实际上最后
b
b
b串就是将排列
p
p
p的第
k
−
(
n
−
k
)
k-(n-k)
k−(n−k)到
k
−
1
k-1
k−1个数翻转到后面去,这样
b
b
b串中第
k
−
(
n
−
k
)
k-(n-k)
k−(n−k)到第
n
n
n的数,形成了一个回文串。
计算(模拟)可以发现,相同长度回文串的逆序数是固定的(不过要是数字组成不同可能不同,本题数字组成都是一样的:也就是左半部分数字都不同)。
那么真正影响逆序数的就是前面
k
−
(
n
−
k
)
−
1
k-(n-k)-1
k−(n−k)−1个数,为了最小化,只能填
1
,
2
,
3..
k
−
(
n
−
k
)
−
1
1,2,3..k-(n-k)-1
1,2,3..k−(n−k)−1,而
k
−
(
n
−
k
)
k-(n-k)
k−(n−k)到
n
n
n的数,则填成
k
,
k
−
1
,
k
−
2...
k
−
(
n
−
k
)
,
k
−
(
n
−
k
)
+
1...
k
k,k-1,k-2...k-(n-k),k-(n-k)+1...k
k,k−1,k−2...k−(n−k),k−(n−k)+1...k这样的形式可以保证字典序最大。
所以结果就是把
1
,
2
,
3
,
4...
k
1,2,3,4...k
1,2,3,4...k中后
n
−
k
+
1
n-k+1
n−k+1个数翻转一下。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const int INF = 0x3f3f3f3f;
int main() {
int T;scanf("%d",&T);
while(T--) {
int n,k;scanf("%d%d",&n,&k);
if(n > k && k > 1) {
for(int i = 1;i <= k - (n - k) - 1;i++) {
printf("%d ",i);
}
for(int i = k;i >= k - (n - k);i--) {
printf("%d ",i);
}
} else {
for(int i = 1;i <= k;i++) {
printf("%d ",i);
}
}
printf("\n");
}
return 0;
}