【CODEFORCES】 B. Long Jumps

B. 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 = 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.
Sample test(s)
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.
문제풀이: 이 문제를 받으면 먼저 답이 2가 아니면 1, 또는 0이 맞는지 확인할 수 있다.답이 0인 경우 가장 잘 판단할 수 있는 것은 두 그룹의 a[i]가 존재하고 a[j]의 차이는 각각 x, y와 같다.a[i]-a[j]=x에 따라 a[i]=a[j]+x를 출시한다.그래서 그중의 하나를 매거한 다음에 다른 하나를 나누어 찾을 수 있다.만약 두 그룹의 a[i], a[j]를 찾지 못하면 먼저 답이 1과 같은 상황을 판단한다(이 경우 a[i]+x=a[j]+y[i]+x=a[j]-ya[i]-x=a[j]-x=a[j]+ya[i]-x=a[j]-x=a[j]-y]x=a[i]x=a[i]와 같은 7가지 상황은 구체적으로 설명하지 않고 대충 계산해 보면 알 수 있다). 만약에 이 모든 상황을 이해하지 못하면 이 두 개의 답이 충족되지 않으면 yx가 된다.
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int a[100005],n,l,x,y,p,q;

int bse(int a[],int l,int r,int k)
{
    if (l==r && a[l]!=k) return 0;
    int t=a[(l+r)/2];
    if (t==k) return (l+r)/2;
    else if (t<k) return bse(a,(l+r)/2+1,r,k);
    else return bse(a,l,(l+r)/2,k);
}

int main()
{
    scanf("%d%d%d%d",&n,&l,&x,&y);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    p=0; q=0;
    for (int i=1;i<=n;i++) if (bse(a,1,n,x+a[i]))
    {
        p++;
        break;
    }
    for (int i=1;i<=n;i++) if (bse(a,1,n,y+a[i]))
    {
        q++;
        break;
    }
    if (p&&q)
    {
        printf("0
"); return 0; } for (int i=1;i<=n;i++) if (bse(a,1,n,x-y+a[i]) && x+a[i]<=l) { printf("1
%d
",x+a[i]); return 0; } for (int i=1;i<=n;i++) if (bse(a,1,n,a[i]+x+y) && a[i]+x<=l) { printf("1
%d
",x+a[i]); return 0; } for (int i=1;i<=n;i++) if (bse(a,1,n,a[i]-x-y) && a[i]-x>=0) { printf("1
%d
",a[i]-x); return 0; } for (int i=1;i<=n;i++) if (bse(a,1,n,a[i]-x+y) && a[i]-x>=0) { printf("1
%d
",a[i]-x); return 0; } if (p) printf("1
%d
",y); else if (q) printf("1
%d
",x); else { for (int i=1;i<=n;i++) if (a[i]==x+y) {p=1; break;} if (p) printf("1
%d
",x); else printf("2
%d %d
",x,y); } return 0; }

좋은 웹페이지 즐겨찾기