Codeforces 479D Long Jumps【map标记+贪心】

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 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 230
0 185 250
Output
1
230
Input
4 250 185 230
0 20 185 250
Output
0
Input
2 300 185 230
0 300
Output
2
185 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.

能够测量一个长度D的要求是刻度尺上有两个位子P1,P2(P2>P1).使得P2-P1==D.

然后给你N个刻度(每个刻度表示这个刻度距离最左端的长度);

问你最少添加多少个刻度能够测量者两种长度x,y;


思路:


1、如果现在有一个刻度a【i】,若数组中存有a【i】+x或者是a【i】-x,那么x这个长度就是可以测量的,y长度同理。

那么我们首先将所有a【i】都存入map中。


2、接下来考虑贪心分类讨论:

①如果原来的刻度能够达到目标,那么结果就是0.

②如果原来的刻度能够达到x这个目标但是达不到y这个目标,那么结果一定就是1.我们找到任意一个刻度,使得其a【i】+y或者是a【i】-y在合法范围内(0~L)即可。

③如果原来的刻度不能够达到x这个目标,但是能够达到y这个目标,那么结果也一定是1.同理我们找到任意一个刻度,使得a【i】+x或者是a【i】-x在合法范围内(0~L)即可。

④如果原来的刻度两个目标都测量不出来。显然答案是 1<=ans<=2的.那么我们暴力判断一下ans==1的结果是否存在,即我们将所有点都遍历一遍,使得在合法范围内可以测量一种长度的那个刻度,能否再通过建立出来的这个刻度和原刻度中的某个Aj构成测量另外一个长度的情况。那么结果就是1.否则就是2.

ans==2的情况,我们只要在原序列中任意取合法范围内的元素即可。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
int a[540000];
int main()
{
    int n,l,x,y;
    while(~scanf("%d%d%d%d",&n,&l,&x,&y))
    {
        map<int , int >s;
        s[0]=1;s[l]=1;
        for(int i=0;i<n;i++)scanf("%d",&a[i]),s[a[i]]=1;
        int flag1=0,flag2=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]+x<=l)
            {
                if(s[a[i]+x]==1)flag1=1;
            }
            if(a[i]-x>=0)
            {
                if(s[a[i]-x]==1)flag1=1;
            }
            if(a[i]+x<=l)
            {
                if(s[a[i]+y]==1)flag2=1;
            }
            if(a[i]-x>=0)
            {
                if(s[a[i]-y]==1)flag2=1;
            }
        }
        if(flag1==1&&flag2==1)printf("0\n");
        else
        {
            if(flag1==0&&flag2==1)
            {
                int ans=-1;
                for(int i=0;i<n;i++)
                {
                    if(a[i]+x<=l)ans=a[i]+x;
                    if(a[i]-x>=0)ans=a[i]-x;
                }
                printf("1\n%d\n",ans);
            }
            if(flag1==1&&flag2==0)
            {
                int ans=-1;
                for(int i=0;i<n;i++)
                {
                    if(a[i]+y<=l)ans=a[i]+y;
                    if(a[i]-y>=0)ans=a[i]-y;
                }
                printf("1\n%d\n",ans);
            }
            if(flag1==0&&flag2==0)
            {
                int ans=-1;
                for(int i=0;i<n;i++)
                {
                    if(a[i]+x<=l)
                    {
                        if(s[a[i]+x+y]==1||s[a[i]+x-y]==1)ans=a[i]+x;
                    }
                    if(a[i]-x>=0)
                    {
                        if(s[a[i]-x+y]==1||s[a[i]-x-y]==1)ans=a[i]-x;
                    }
                    if(a[i]+y<=l)
                    {
                        if(s[a[i]+y+x]==1||s[a[i]+y-x]==1)ans=a[i]+y;
                    }
                    if(a[i]-y>=0)
                    {
                        if(s[a[i]-y+x]==1||s[a[i]-y-x]==1)ans=a[i]-y;
                    }
                }
                if(ans==-1)
                {
                    int ans1,ans2;
                    for(int i=0;i<n;i++)
                    {
                        if(a[i]+x<=l)ans1=a[i]+x;
                        if(a[i]-x>=0)ans1=a[i]-x;
                        if(a[i]+y<=l)ans2=a[i]+y;
                        if(a[i]-y>=0) ans2=a[i]-y;
                    }
                    printf("2\n%d %d\n",ans1,ans2);
                }
                else printf("1\n%d\n",ans);
            }
        }
    }
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值