CF1844F1.Min Cost Permutation (Easy Version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The only difference between this problem and the hard version is the constraints on t and n .
You are given an array of n positive integers a1,…,an , and a (possibly negative) integer c .
Across all permutations b1,…,bn of the array a1,…,an , consider the minimum possible value of $$$$\sum_{i=1}^{n-1} |b_{i+1}-b_i-c|. $$ Find the lexicographically smallest permutation b of the array a that achieves this minimum.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
- x is a prefix of y , but xney ;
- in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y$$.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤103 ). The description of the test cases follows.
The first line of each test case contains two integers n and c ( 1≤n≤5⋅103 , −109≤c≤109 ).
The second line of each test case contains n integers a1,…,an ( 1≤ai≤109 ).
It is guaranteed that the sum of n over all test cases does not exceed 5⋅103 .
输出格式
For each test case, output n integers b1,…,bn , the lexicographically smallest permutation of a that achieves the minimum i=1∑n−1∣bi+1−bi−c∣ .
输入输出样例
输入#1
3 6 -7 3 1 4 1 5 9 3 2 1 3 5 1 2718 2818
输出#1
9 3 1 4 5 1 1 3 5 2818
说明/提示
In the first test case, it can be proven that the minimum possible value of i=1∑n−1∣bi+1−bi−c∣ is 27 , and the permutation b=[9,3,1,4,5,1] is the lexicographically smallest permutation of a that achieves this minimum: ∣3−9−(−7)∣+∣1−3−(−7)∣+∣4−1−(−7)∣+∣5−4−(−7)∣+∣1−5−(−7)∣=1+5+10+8+3=27 .
In the second test case, the minimum possible value of i=1∑n−1∣bi+1−bi−c∣ is 0 , and b=[1,3,5] is the lexicographically smallest permutation of a that achieves this.
In the third test case, there is only one permutation b .