CF1774H.Maximum Permutation
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Ecrade bought a deck of cards numbered from 1 to n . Let the value of a permutation a of length n be i=1minn−k+1 j=i∑i+k−1aj .
Ecrade wants to find the most valuable one among all permutations of the cards. However, it seems a little difficult, so please help him!
输入格式
The first line contains a single integer t ( 1≤t≤2⋅104 ) — the number of test cases. The description of test cases follows.
The only line of each test case contains two integers n,k ( 4≤k<n≤105 ).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅106 .
输出格式
For each test case, output the largest possible value in the first line. Then in the second line, print n integers a1,a2,…,an ( 1≤ai≤n , all ai are distinct) — the elements of the permutation that has the largest value.
If there are multiple such permutations, output any of them.
输入输出样例
输入#1
2 5 4 8 4
输出#1
13 1 3 4 5 2 18 1 8 2 7 3 6 4 5
说明/提示
In the first test case, [1,4,5,3,2] has a value of 13 . It can be shown that no permutations of length 5 have a value greater than 13 when k=4 .
In the second test case, [4,2,5,7,8,3,1,6] has a value of 18 . It can be shown that no permutations of length 8 have a value greater than 18 when k=4 .