A16002 | Min Cost Permutation (Hard Version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The only difference between this problem and the easy version is the constraints on $t$ and $n$ .
You are given an array of $n$ positive integers $a_1,\dots,a_n$ , and a (possibly negative) integer $c$ .
Across all permutations $b_1,\dots,b_n$ of the array $a_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 $b$ of the array $a$ that achieves this minimum.</p><p>A sequence $x$ is lexicographically smaller than a sequence $y$ if and only if one of the following holds:</p><ul><li> $x$ is a prefix of $y$ , but $x \\ne y$ ;</li><li> in the first position where $x$ and $y$ differ, the sequence $x$ has a smaller element than the corresponding element in $y$$$.
You are given an array of $n$ positive integers $a_1,\dots,a_n$ , and a (possibly negative) integer $c$ .
Across all permutations $b_1,\dots,b_n$ of the array $a_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 $b$ of the array $a$ that achieves this minimum.</p><p>A sequence $x$ is lexicographically smaller than a sequence $y$ if and only if one of the following holds:</p><ul><li> $x$ is a prefix of $y$ , but $x \\ne y$ ;</li><li> 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 \le t \le 10^4$ ). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $c$ ( $1 \le n \le 2 \cdot 10^5$ , $-10^9 \le c \le 10^9$ ).
The second line of each test case contains $n$ integers $a_1,\dots,a_n$ ( $1 \le a_i \le 10^9$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
The first line of each test case contains two integers $n$ and $c$ ( $1 \le n \le 2 \cdot 10^5$ , $-10^9 \le c \le 10^9$ ).
The second line of each test case contains $n$ integers $a_1,\dots,a_n$ ( $1 \le a_i \le 10^9$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, output $n$ integers $b_1,\dots,b_n$ , the lexicographically smallest permutation of $a$ that achieves the minimum $\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 $\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-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 $\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-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$ .
In the second test case, the minimum possible value of $\sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted