B. Long Jumps
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/480/problem/B
Description
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.
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.
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.
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 Input
3 250 185 230
0 185 250
Sample Output
1
230
HINT
题意
给你一个尺子,上面已经有了n个刻度,这把尺子最长l,然后问你最少加几个刻度,就可以测出x和y了
题解:
答案肯定只有0,1,2这三种
答案为0的就很简单,那么就是x存在或者y存在,或者k和k+x同时存在,k和k+y也同时存在
答案为2的也很简单,就把x,y直接输出就好了
答案为1的讨论一下:
1.x或者y其中一个可以由尺子构成
2.尺子上存在k-x-y,那么我们可以构造一个k-x或者k-y就行了
3.尺子上存在k-(y-x),那么我们可以构造k+x,或者k-y就行了
代码:
#include<iostream> #include<stdio.h> #include<map> using namespace std; #define maxn 100005 map<int,int> H; int main() { int n,l,x,y; scanf("%d%d%d%d",&n,&l,&x,&y); if(x>y)swap(x,y); int flag1=0,flag2=0,flag3=0; for(int i=1;i<=n;i++) { int k;scanf("%d",&k); H[k]=1; if(H[x])flag1=1; if(H[k-x])flag1=1; if(H[k+x])flag1=1; if(H[k-y])flag2=1; if(H[k+y])flag2=1; if(H[y])flag2=1; if(H[k-x-y])flag3=k-x; if(H[k-(y-x)]) { if(k+x<=l)flag3=k+x; else if(k-y>=0)flag3=k-y; } } if(flag1&&flag2)printf("0 "); else if(flag1)printf("1 %d ",y); else if(flag2)printf("1 %d ",x); else if(flag3)printf("1 %d ",flag3); else printf("2 %d %d ",x,y); }