Codeforces-1473 C. No More Inversions(回文串逆序数)

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,k1,k2,...,k(nk),
求一个排列 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(nk) k − 1 k-1 k1个数翻转到后面去,这样 b b b串中第 k − ( n − k ) k-(n-k) k(nk)到第 n n n的数,形成了一个回文串。
计算(模拟)可以发现,相同长度回文串的逆序数是固定的(不过要是数字组成不同可能不同,本题数字组成都是一样的:也就是左半部分数字都不同)。
那么真正影响逆序数的就是前面 k − ( n − k ) − 1 k-(n-k)-1 k(nk)1个数,为了最小化,只能填 1 , 2 , 3.. k − ( n − k ) − 1 1,2,3..k-(n-k)-1 1,2,3..k(nk)1,而 k − ( n − k ) k-(n-k) k(nk) 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,k1,k2...k(nk),k(nk)+1...k这样的形式可以保证字典序最大。
所以结果就是把 1 , 2 , 3 , 4... k 1,2,3,4...k 1,2,3,4...k中后 n − k + 1 n-k+1 nk+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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值