CF_479D Long Jumps(二分)

来源:互联网 发布:天才 知乎 编辑:程序博客网 时间:2024/04/28 01:39
D. Long Jumps
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nmarks, 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 = 0an = 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 nlxy (2 ≤ n ≤ 1052 ≤ l ≤ 1091 ≤ 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.

Sample test(s)
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.


题意:

体育课要考试,一老师拿了一把长度为l的尺子,尺子上标有一些刻度,他要求女生能跳x,男生能跳y(y>x).但是这些刻度不一定能满足测量的要求,告诉你这些刻度,让你判断一下最多需要添加几个点。理解题意很重要,有几点需要注意一下。

1.一共只需要两个刻度,所以需要的点数只有0,1,2三种可能。

2.所需要的刻度也可以由ai-aj得到。

3.多组解符合情况输出任何一组都可以。

题解:

题目看起来是很简单的,但是却又感觉无从下手。先来分析一下所有情况:

1.两个距离都能找到,存在某几个点之间距离差恰好等于x,y,添加0个点;

2.只能找到一个距离x或y,添加1个点;

3.一个都找不到,但是两个点能共用一个点,只需添加一个公用点。共用一个点的话有两种情况,一种是公用点在三点中间,则能在原刻度上找到两个点使两点间距离满足x+y,另一种情况是公用点在两边,则能在原刻度上找到两个点间距离满足y-x,这种情况在左右添加公共点都可以,但是一定注意不要越过边界。此时添加1个点;

4.其他,添加2个点;

情况分析好了之后就很简单了,从前向后遍历,找四个距离,根据查找结果输出答案就好了。因为只差照一次就可以,所以时间上也快了很多。但是有一点需要注意,x,y,x+y都没问题,只要能找到一定能符合情况,但y-x就需要另外判断一下,只满足差值但是往两边添加点时却有可能越界。一定要在查找的时候去判断,比如有两组点满足差值为y-x,如果你在查找到第一组时就跳出,可能第一组是两边都会越界的,此时第二组右边虽然一定越界但左边却可能会满足,所以一定要放在查找的时候去判断。因为这个点wa了无数次,一定要注意。

代码实现:

<span style="font-size:14px;">#include <iostream>#include <stdio.h>#include <stdlib.h>#include <cstring>#include <algorithm>#define INF 1000000009#define MAX 100005using namespace std;int a[MAX];bool solve();//主实现函数int finds(int);//查找函数int n,length,x,y;int main(){    scanf("%d%d%d%d",&n,&length,&x,&y);    memset(a,0,sizeof(a));    for(int i=0;i<n;i++)        scanf("%d",&a[i]);    //主实现函数    solve();    return 0;}bool solve(){    //定义四个变量储存返回结果,找不到返回-1,找得到返回右侧的点    int r1,r2,r3,r4;    //查找四个距离    r1=finds(x);    r2=finds(y);    r3=finds(x+y);    r4=finds(y-x);    //x,y都找到    if(r1!=-1&&r2!=-1)    {        printf("0\n");    }    else    {        //仅x找到        if(r1!=-1)            printf("%d\n%d\n",1,y);        //仅y找到        else if(r2!=-1)            printf("%d\n%d\n",1,x);        //x+y找到        else if(r3!=-1)            printf("%d\n%d\n",1,r3-y);        //y-x找到        else if(r4!=-1)        {            if(r4-y>0)                printf("%d\n%d\n",1,r4-y);            else if(r4+x<length)                printf("%d\n%d\n",1,r4+x);        }        //都找不到        else            printf("%d\n%d %d\n",2,x,y);    }    return true;}int finds(int pos){    int tmp;    for(int i=0;i<n;i++)    {        //二分查找满足距离的点        tmp=lower_bound(a,a+n,a[i]+pos)-a;        //找不到        if(tmp==n)            break;        else        {            //满足距离的点存在            if(a[tmp]==a[i]+pos)            {                //距离为y-x时要格外注意                if(pos==y-x)                {                    if(a[tmp]-y<0&&a[tmp]+x>length)                        continue;                    else                        return a[tmp];                }                //返回右测点的位置                else                    return a[tmp];            }        }    }    //不满足返回-1    return -1;}</span>


0 0
原创粉丝点击