CodeChef:Consecutive Snakes(三分)

来源:互联网 发布:机械力学分析软件 编辑:程序博客网 时间:2024/05/29 04:09

It's the annual military parade, and all the soldier snakes have arrived at the parade arena, But they aren't standing properly. The entire parade must be visible from the main podium, and all the snakes must be in a line. But the soldiers are lazy, and hence you must tell the soldiers to move to their new positions in such a manner that the total movement is minimized.

Formally, the entire parade strip can be thought of as the integer line. There are N snakes, where each snake is a line segment of length L. The i-th snake is initially at the segment [Si, Si + L]. The initial positions of the snakes can overlap. The only segment of the strip visible from the podium is [A, B], and hence all the snakes should be moved so that all of them are visible from the podium. They should also all be in a line without gaps and every consecutive pair touching each other. In other words, they should occupy the segments [X, X + L], [X + L, X + 2*L], ... , [X + (N-1)*L, X + N*L], for some X, such that A ≤ X ≤ X + N*L ≤ B. You are guaranteed that the visible strip is long enough to fit all the snakes.

If a snake was initially at the position [X1, X1 + L] and finally is at the position [X2, X2 + L], then the snake is said to have moved a distance of |X2 - X1|. The total distance moved by the snakes is just the summation of this value over all the snakes. You need to move the snakes in such a manner that it satisfies all the conditions mentioned above, as well as minimize the total distance. You should output the minimum total distance achievable.

Input

  • The first line contains a single integer, T, the number of testcases. The description of each testcase follows.
  • The first line of each testcase contains four integers, N, L, A and B, where N denotes the number of snakes, L denotes the length of each snake, and [A, B] is the segment visible from the podium.
  • The next line contains N integers, the i-th of which is Si. This denotes that the i-th snake is initially in the segment [Si, Si + L].

Output

  • For each testcase, output a single integer in a new line: the minimum total distance achievable.

Constraints

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 105
  • 1 ≤ Si ≤ 109
  • 1 ≤ L ≤ 109
  • 1 ≤ A ≤ B ≤ 109
  • N * L ≤ B - A

Example

Input:23 4 11 2310 11 303 4 11 4010 11 30Output:1616

Explanation

In the first testcase, the three snakes are initially at segments [10, 14], [11, 15], and [30, 34]. One optimal solution is to move the first snake which was at [10, 14] to [15, 19] and the third snake which was at [30, 34] to [19, 23]. After this, the snakes would form a valid parade because they will be from [11, 15], [15, 19] and [19, 23]. Hence they are all in a line without any gaps in between them, and they are all visible, because they all lie in the visible segment, which is [11, 23].

The distance traveled by the first snake is |15 - 10| = 5, by the second snake is |11 - 11| = 0 and by the third snake is |19 - 30| = 11. Hence the total distance traveled is 5 + 0 + 11 = 16. This is the best that you can do, and hence the answer is 16.

In the second testcase, only the visible segment has increased. But you can check that the same final configuration as in the first subtask is still optimal here. Hence the answer is 16.

题意:在年度阅兵中,所有的士兵蛇都在阅兵场集合了,但这些蛇的站位不对。整场阅兵必须能从主席台看清楚,所有蛇都应该站成一排。但这些士兵非常懒惰,你必须指挥士兵重新排队,使得所有人的移动距离之和最短。形式化地,整支阅兵队伍可以视作一条数轴。共有 N 条蛇,每条蛇是一根长度为 L 的线段。第 i 条蛇初始时占据了 [Si, Si + L] 的区间,蛇与蛇之间可以重合。能从主席台看到的只有区间[A, B],因此所有的蛇都应该处于这个区间内。蛇应该排列成连续一段,之间不能留缝隙,即这些蛇从前往后应该一次占据 [X, X + L], [X + L, X + 2L], . . . , [X + (N − 1)L, X + NL] 这些区间,其中 A ≤ X ≤ X + NL ≤ B。保证 [A, B] 足以排下所有的蛇。如果一条蛇初始时位于 [X1, X1 + L],重排后位于 [X2, X2 + L],那么这条蛇的移动距离为|X2 − X1|。你需要将蛇重新排列,使得排列后满足上述条件,并最小化所有蛇的移动距离之和。你需要输出最短距离和。输入格式输入的第一行包含一个整数 T,代表测试数据的组数。接下来是 T 组数据。每组数据第一行包含四个整数 N、L、A 和 B,分别代表蛇的条数、蛇的长度,以及从主席台可见的区间 [A, B]。接下来一行包含 N 个整数 S1, S2, . . . , SN,代表每条蛇初始所处区间的左端点。输出格式对于每组数据,输出一行,包含一个整数,代表最短距离和。

思路:枚举蛇的起点即可,可以看出这是一个凸函数,故用三分找出最优的起点。

//reference: cillyb# include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 1e5+3;LL t, n, L, a, b, arr[maxn];LL judge(LL x){    LL sum = 0;    for(int i=1; i<=n; ++i)        sum += llabs(arr[i]-x), x+=L;    return sum;}int main(){    scanf("%d",&t);    while(t--)    {        scanf("%lld%lld%lld%lld",&n,&L,&a,&b);        for(int i=1; i<=n; ++i) scanf("%lld",&arr[i]);        sort(arr+1, arr+1+n);        LL l=a, r=b-L*n+1;        while(r-l > 2)        {            LL l_mid = l+(r-l)/3;            LL r_mid = l+(r-l)*2/3;            if(judge(l_mid) <= judge(r_mid))                r = r_mid;            else                l = l_mid;        }        LL ans = judge(l);        for(int i=l+1; i<=r; ++i)            ans = min(ans, judge(i));        printf("%lld\n",ans);    }    return 0;}