Description
Given an integer sequence X1,X2,X3,…,XN and three positive integers Q,A,B that satisfy:
- 1≤Xi≤Q for any 1≤i≤N;
- Xi≤Xi+1 for any 1≤i<N;
- A≤N−1Q−1 and A≤B.
For any 1≤i≤N, perform the transformation Yi=Xi+δi, where δi is an integer, such that the new sequence Y satisfies:
- 1≤Yi≤Q for any 1≤i≤N;
- Yi+1−Yi∈[A,B] for any 1≤i<N.
For such a transformation, the required cost is defined as $\operatorname{TransformCost}(X, Y) = \sum_{i = 1}^{N}{\left\lvert\delta_i\right\rvert}$.
The task is to find a transformation that minimizes TransformCost(X,Y).
The first line contains 4 integers N,Q,A,B.
The second line contains N integers, namely X1,X2,…,XN.
Output a single line with the minimal TransformCost(X,Y).
3 6 2 2
1 4 6
1
Hint
Sample Explanation.
You can transform the sequence to 2 4 6 or 1 3 5. The former has a cost of 1, and the latter has a cost of 2. Therefore, the minimal TransformCost is 1.
Constraints.
For 10% of the testdata, N≤100, Q≤10000, 1≤A,B≤100.
For 30% of the testdata, N≤10000, Q≤10000, 1≤A,B≤100.
For 60% of the testdata, N≤10000, Q≤109, 1≤A,B≤Q.
For 100% of the testdata, N≤500000, Q≤109, 1≤A,B≤Q.
Translated by ChatGPT 5