Long Jumps CodeForces - 479D

E - Long Jumps

CodeForces - 479D

Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler!

However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l).

Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d).

Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children‘s abilities, Valery needs a ruler to measure each of the distances x and y.

Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.

Input

The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.

The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin.

Output

In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler.

In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.

Examples

Input

3 250 185 2300 185 250

Output

1230

Input

4 250 185 2300 20 185 250

Output

0

Input

2 300 185 2300 300

Output

2185 230

Note

In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.

In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don‘t have to add new marks.

In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children‘s skills.

OJ-ID:
CodeForces-479D

author:
Caution_X

date of submission:
20191109

tags:
二分,贪心

description modelling:
有一把尺子,尺子上有n个刻度A[i],问能否通过已知的刻度测出长度x和长度y?
输出需要补充的刻度点个数和对应的值

major steps to solve it:
需要补充的刻度点数只能是0,1,2
需要补充的点数为0时可以直接判断
判断能否只补充一个刻度点:①记tx=A[i]+x,表示可以在A[i]右边得出一个刻度能够测出x
再二分查找判断(tx+y)或者(tx-y)在不在已知刻度中,若在,则一个刻度点tx即可,同理,②记ty=A[i]+y,
重复类似①的操作,只要①,②有一个满足条件即可,若都不满足时:记tx=A[i]-x,表示能够在
刻度点A[i]左边找到一个刻度点测出x,记ty=A[i]-y,同理操作。若通过上述操作能够找出,则
只需要补充一个刻度点,否则,需要补充两个刻度点。

warnings:
重点在于点数1和点数2的判断,点数1需要特判
比如x=6,y=7,已知的刻度点有4,5,那么只要补充一个刻度点11即可

AC code:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1e5+5;
int N, L,  X, Y, A[maxn];

bool judge (int u) {
    if (u < 0 || u > L) return false;
    int k = lower_bound(A, A + N, u) - A;
    return u == A[k];
}

void solve () {
    int ans = 0;
    for (int i = 0; i < N; i++) {
        if (judge(A[i] - X) || judge(A[i] + X))
            ans |= 1;
        if (judge(A[i] - Y) || judge(A[i] + Y))
            ans |= 2;
    }

    if (ans == 3)
        printf("0\n");
    else if (ans == 2)
        printf("1\n%d\n", X);
    else if (ans == 1)
        printf("1\n%d\n", Y);
    else {

        for (int i = 0; i < N; i++) {
            int tx = A[i] + X;
            int ty = A[i] + Y;

            if (tx <= L && (judge(tx - Y) || judge(tx + Y))) {
                printf("1\n%d\n", tx);
                return;
            }

            if (ty <= L && (judge(ty - X) || judge(ty + X))) {
                printf("1\n%d\n", ty);
                return;
            }
        }

        for (int i = 0; i < N; i++) {
            int tx = A[i] - X;
            int ty = A[i] - Y;

            if (tx >= 0 && (judge(tx - Y) || judge(tx + Y))) {
                printf("1\n%d\n", tx);
                return;
            }

            if (ty >= 0 && (judge(ty - X) || judge(ty + X))) {
                printf("1\n%d\n", ty);
                return;
            }
        }
        printf("2\n%d %d\n", X, Y);
    }
}

int main () {
    scanf("%d%d%d%d", &N, &L, &X, &Y);
    for (int i = 0; i < N; i++)
        scanf("%d", &A[i]);
    solve();
    return 0;
}

原文地址:https://www.cnblogs.com/cautx/p/11828275.html

时间: 2024-11-06 11:22:14

Long Jumps CodeForces - 479D的相关文章

Codeforces 479D Long Jumps(贪心+二分)

题目链接:Codeforces 479D Long Jumps 题目大意:valery是个体育老师,现在他要为学生考跳远,女生标准为x,男生为y,现在一个长为L的刻度尺,有N个刻 度,给定N个刻度,现在为说还需要加几个刻度才能测量x,y这两个长度. 解题思路:因为总共就x,y两个长度,所以最多加两个刻度.所以只要判断不加和加一个的情况即可. 先枚举每个刻度a[i],然后用二分查找一下a[i]+x或者a[i]-x刻度存不存在,同理y.如果x和y都通过判断,那么就是不需 要加刻度. 如果只通过x或只

Codeforces 479D - Long Jumps

479D - Long Jumps, 480B - Long Jumps It is easy to see that the answer is always 0, 1 or 2. If we can already measure both x and y, output 0. Then try to measure both x and y by adding one more mark. If it was not successful, print two marks: one at

【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps

我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是,我们现在只需要统计sigma((L+f(L,K))),最后除以K即可. 统计sigma(L)时,我们考虑计算每条边出现在了几条路径中,设u为edgei的子节点,那么这条边对答案的贡献就是siz(u)*(n-siz(u)),siz(u)为u的子树大小. 统计sigma(f(L,K))时,我们需要dp出

codeforces 1324 C. Frog Jumps(贪心/二分)

There is a frog staying to the left of the string s=s1s2…sns=s1s2…sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00 ). Each character of ss is either 'L' or 'R'. It means that if the frog is staying at the ii

Codeforces 480B Long Jumps 规律题

题目链接:点击打开链接 题意: 输出n l x y 有一根直尺长度为l 上面有n个刻度. 下面n个数字是距离开头的长度(保证第一个数字是0,最后一个数字是l) 要使得 直尺中存在某2个刻度的距离为x , 某2个刻度的距离为y 要添加最少几个刻度. 问: 最少的刻度个数 输出标记的位置. 思路: 分类讨论一下.. 若本身尺子里就有x.y就输出0 若只有x 或只有y就输出一个刻度. 若2个都没有就: 1.加1个刻度ans,这个ans是距离某个刻度距离为x的,然后看一下是否有距离ans为y的刻度,若有

CF 479D Long Jumps

#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; int a[100010]; int n; bool find(int x) { int l = 0, r = n-1; while(l <= r) { int m = (l+r) >> 1; if(a[m] == x) return true; i

Codeforces 602B Approximating a Constant Range(想法题)

B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a suffi

Codeforces Round #105 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/148 比较简单的一场,最长的一题也才写了30行多一点 A. Insomnia cure time limit per test:2 seconds memory limit per test:256 megabytes ?One dragon. Two dragon. Three dragon?, - the princess was counting. She had trouble falling asleep, and

Codeforces 148D Bag of mice (概率dp)

D. Bag of mice time limit per test:2 seconds memory limit per test:256 megabytes The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, wh