CF1844F1.Min Cost Permutation (Easy Version)

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The only difference between this problem and the hard version is the constraints on tt and nn.

You are given an array of nn positive integers a1,…,ana_1,\dots,a_n, and a (possibly negative) integer cc.

Across all permutations b1,…,bnb_1,\dots,b_n of the array a1,…,ana_1,\dots,a_n, consider the minimum possible value of $$\sum_{i=1}^{n-1} |b_{i+1}-b_i-c|.$$ Find the lexicographically smallest permutation bb of the array aa that achieves this minimum.

A sequence xx is lexicographically smaller than a sequence yy if and only if one of the following holds:

  • xx is a prefix of yy, but x≠yx \ne y;
  • in the first position where xx and yy differ, the sequence xx has a smaller element than the corresponding element in yy.

本题与困难版本的唯一区别在于 tt 和 nn 的约束条件。

给定一个由 nn 个正整数组成的数组 a1,…,ana_1,\dots,a_n,以及一个(可能为负的)整数 cc。

在数组 a1,…,ana_1,\dots,a_n 的所有排列 b1,…,bnb_1,\dots,b_n 中,考虑表达式

∑i=1n−1∣bi+1−bi−c∣\sum_{i=1}^{n-1} |b_{i+1}-b_i-c|

所能取到的最小值。请找出达到该最小值的、字典序最小的数组 aa 的排列 bb。

序列 xx 字典序小于序列 yy,当且仅当满足以下条件之一:

  • xx 是 yy 的真前缀(即 xx 是 yy 的前缀且 x≠yx \ne y);
  • 在 xx 与 yy 首次出现不同元素的位置上,xx 中对应位置的元素小于 yy 中对应位置的元素。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1031 \le t \le 10^3). The description of the test cases follows.

The first line of each test case contains two integers nn and cc (1≤n≤5⋅1031 \le n \le 5 \cdot 10^3, −109≤c≤109-10^9 \le c \le 10^9).

The second line of each test case contains nn integers a1,…,ana_1,\dots,a_n (1≤ai≤1091 \le a_i \le 10^9).

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1035 \cdot 10^3.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1031 \le t \le 10^3)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 cc(1≤n≤5⋅1031 \le n \le 5 \cdot 10^3,−109≤c≤109-10^9 \le c \le 10^9)。

每个测试用例的第二行包含 nn 个整数 a1,…,ana_1,\dots,a_n(1≤ai≤1091 \le a_i \le 10^9)。

保证所有测试用例的 nn 值之和不超过 5⋅1035 \cdot 10^3。

输出格式

For each test case, output nn integers b1,…,bnb_1,\dots,b_n, the lexicographically smallest permutation of aa that achieves the minimum ∑i=1n−1∣bi+1−bi−c∣\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c|.

对于每个测试用例,输出 nn 个整数 b1,…,bnb_1,\dots,b_n,即数组 aa 的字典序最小的排列,使得 ∑i=1n−1∣bi+1−bi−c∣\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-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=1n−1∣bi+1−bi−c∣\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| is 2727, and the permutation b=[9,3,1,4,5,1]b = [9,3,1,4,5,1] is the lexicographically smallest permutation of aa 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|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=1n−1∣bi+1−bi−c∣\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| is 00, and b=[1,3,5]b = [1,3,5] is the lexicographically smallest permutation of aa that achieves this.

In the third test case, there is only one permutation bb.

在第一个测试用例中,可以证明 ∑i=1n−1∣bi+1−bi−c∣\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| 的最小可能值为 2727,且排列 b=[9,3,1,4,5,1]b = [9,3,1,4,5,1] 是达到该最小值的 aa 的字典序最小排列:∣3−9−(−7)∣+∣1−3−(−7)∣+∣4−1−(−7)∣+∣5−4−(−7)∣+∣1−5−(−7)∣=1+5+10+8+3=27|3-9-(-7)|+|1-3-(-7)|+|4-1-(-7)|+|5-4-(-7)|+|1-5-(-7)| = 1+5+10+8+3 = 27。

在第二个测试用例中,∑i=1n−1∣bi+1−bi−c∣\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| 的最小可能值为 00,且 b=[1,3,5]b = [1,3,5] 是达到该最小值的 aa 的字典序最小排列。

在第三个测试用例中,仅存在唯一一个排列 bb。

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

首页