Public Judge

pjudge

Time Limit: 2 s Memory Limit: 512 MB Total points: 100

#21807. 【PR #12】电塔

الإحصائيات

题目描述

今天是 YQH 的生日,她得到了一套益智玩具—— $n$ 个电塔作为生日礼物。所有电塔位于同一条直线上。具体的,把电塔所在直线抽象为一条数轴的非负半轴,那么第 $i$ 个电塔位于数轴上 $x_i$ 的位置。

假如两个电塔之间的相对距离严格小于 $d$,那么它们就会放电。在游玩一段时间后,YQH 感到厌倦了,于是她准备把这些电塔收拾起来。为了不浪费电,她希望把电塔调到都不放电的状态。

具体的,YQH 每次可以把某个电塔沿数轴正方向移动一个单位或沿数轴负方向移动一个单位,但是必须保证电塔位于数轴的非负半轴。你可以认为其他电塔不会干扰电塔的移动。

移动电塔是一个很累的行为,所以 YQH 希望求出移动电塔的最小次数,使得所有电塔都不放电。

输入格式

本题采用多组测试,第一行一个整数 $T$ 表示数据组数。

对于每组数据:

第一行两个正整数 $n,d$。

第二行 $n$ 个非负整数 $x_i$,表示电塔的位置。

输出格式

对于每组数据,输出一行一个整数表示答案。

样例

样例输入1

2
4 1
0 0 0 0 
4 10
1 100 5 10

样例输出1

6
16

对于第一组数据,考虑电塔最终的位置 $x^\prime=[0,1,2,3]$,显然满足全部电塔不放电的要求,移动次数为 $\sum|x_i-x^\prime_i|=6$,可以证明没有更优的解。

对于第二组数据,考虑电塔最终的位置 $x^\prime=[0,100,20,10]$,移动次数为 $\sum|x_i-x^\prime_i|=16$,可以证明没有更优的解。

样例输入/输出2

见下发文件中 ex_ele2.inex_ele2.ans

样例输入/输出3

见下发文件中 ex_ele3.inex_ele3.ans

数据范围与提示

对于全部数据,保证 $1\le T\le 10^5,1\le n\le 2\times 10^5,1\le d\le 10^6,0\le x_i\le 3\times 10^{11},\sum n\le 10^6$。

$\text{subtask1 20pts}$,$n\le 10,x_i\le 1000,T=1$。

$\text{subtask2 20pts}$,$d=1$。

$\text{subtask3 10pts}$,$n\le 200,T\le 5$。

$\text{subtask4 20pts}$,$n\le 2000,T\le 5$。

$\text{subtask5 30pts}$,无特殊限制。

时间限制:$\texttt{2s}$

空间限制:$\texttt{512MB}$

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.