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.
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.
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.
3 250 185 230 0 185 250
1 230
4 250 185 230 0 20 185 250
0
2 300 185 230 0 300
2 185 230
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,有n个刻度,问你能不能量出x和 y(x<y)的值,如果不行,就加刻度上去,最少加几个刻度上去。
很明显就两个数最多加两个刻度上去,那么首先判断x和y能否被量出,可以是两个刻度相减,或者两个刻度相加得到x,y。那么用到了set 里的count(n) 返回n在数据集里存在的个数,因为set里的数据都是唯一的,所以返回值只有0,1。
那么遍历刻度,如果a[i]+x 存在,即表示有一个刻度P存在P-a[i]==x;如上题红色部分的解释,d只能是两个刻度相减得到,所以判断x,y是否存在的时的时候只需要判断a[i]+x,a[i]+y是否存在就好。
如果只有一个存在,加上另一个刻度就好,如果两个都不存在就要判断是否可以加一个刻度可以量出两个来,那么就要判断 一下
首先判断第一种情况,加上刻度P=a[i]+x,判断一下a[i]+x-y 或者a[i]+x+y得刻度是否存在
然后判断P=a[i]-x,判断a[i]-x+y和a[i]-x-y得刻度是否存在。
另附上几组容易出错的数据:
Input
4 300 4 5
0 6 7 300
Answer
1
11
2 100 30 70
0 100
1
30
4 300 4 5
0 298 299 300
1
294
19 180 117 148
0 1 19 20 21 28 57 65 68 70 78 88 100 116 154 157 173 179 180
2
117 148
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
set<int> s;
int a[111111];
int n,l;
int x,y;
int find(int x)
{
<span style="white-space:pre"> </span>for(int i=0;i<n;i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if(a[i]+x<=l && s.count(a[i]+x) )return 1;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return 0;
}
int check()
{
<span style="white-space:pre"> </span>int i,j;
<span style="white-space:pre"> </span>for(i=0;i<n;i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if(a[i]+x<=l && (s.count(a[i]+x+y) || s.count(a[i]+x-y)))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>cout<<"RR"<<endl;
<span style="white-space:pre"> </span>return a[i]+x;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span> if(a[i]-x>=0 && (s.count(a[i]-x+y)|| s.count(a[i]-x-y)))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>cout<<"EEE"<<endl;
<span style="white-space:pre"> </span>return a[i]-x;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return -1;
}
int main()
{
//<span style="white-space:pre"> </span>freopen("q.in","r",stdin);
<span style="white-space:pre"> </span>int i,j;
<span style="white-space:pre"> </span>while(~scanf("%d%d%d%d",&n,&l,&x,&y))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>for(i=0;i<n;i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>scanf("%d",&a[i]);
<span style="white-space:pre"> </span>s.insert(a[i]);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span> int t1=find(x),t2=find(y);
<span style="white-space:pre"> </span> if(t1 && t2)
<span style="white-space:pre"> </span> {
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<"0"<<endl;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> else if(t1 && !t2)
<span style="white-space:pre"> </span> {
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<"1"<<endl;
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<y<<endl;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> else if(t2 && !t1)
<span style="white-space:pre"> </span> {
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<"1"<<endl;
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<x<<endl;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> else
<span style="white-space:pre"> </span> {
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>int res=check();
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>if(res!=-1)
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>cout<<"RRRR"<<endl;
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<"1"<<endl;
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<res<<endl;
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<"2"<<endl;
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>cout<<x<<" "<<y<<endl;
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>}
}