您的位置:首页 > 其它

【Codeforces Round 274 (Div 2)D】【STL-SET 讨论】Long Jumps 刻度尺最多加几个刻度使得可以测量X与Y

2015-12-08 15:58 465 查看
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 = 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 2300 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.

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,ms63=1061109567;
int n,m;
LL X,Y;
set<int>sot;
int main()
{
while(~scanf("%d%d%lld%lld",&n,&m,&X,&Y))
{
sot.clear();
for(int i=1;i<=n;++i)
{
int x;scanf("%d",&x);
sot.insert(x);
}
bool findX=0;
bool findY=0;
int Together=0;
for(set<int>::iterator it=sot.begin();it!=sot.end();++it)
{
int x=*it;
if(sot.count(x+X))findX=1;
if(sot.count(x+Y))findY=1;
if(sot.count(x+X+Y))Together=x+X;
else if(sot.count(x+Y-X)&&x+Y<=m)Together=x+Y;
else if(sot.count(x-Y+X)&&x-Y>=0)Together=x-Y;
}
if(findX&&findY){puts("0");continue;}
int ans=0;
if(findX)ans=Y;
else if(findY)ans=X;
else if(Together)ans=Together;
if(ans)
{
puts("1");
printf("%d\n",ans);
}
else
{
puts("2");
printf("%lld %lld\n",X,Y);
}
}
return 0;
}
/*
【trick&&吐槽】
有讨论性质的问题一定不要局限在固有的方案和思维逻辑中。
一定要保证思维的严谨,不漏任何情况!

【题意】
有一个尺子,总长度为m(2<=m<=1e9)。
尺子上共有n(2<=n<=1e5)个刻度,我们按顺序告诉你所有刻度。
数据保证——第一个刻度是0,最后一个刻度是m。
我们想要测量两个长度X,Y(1<=X<Y<=m)。
问你,至少要添加几个刻度,才能实现我们的目的。并输出所有添加的刻度。

【类型】
STL-set 讨论

【分析】
这题,我们可以把所有的点就加进set中,
然后枚举一个点x,
去查找是否有x+X,是否有x+Y,如果两者都有,那么答案就是0。否则答案肯定至少为1。
再去查找是否有x+X+Y,是否有x+Y-X,如果两者有其一,那么答案就是1?
No no no。我们还要考虑实际,考虑尺子总长度只有m。
对于x+Y-X,我们是想要添加x+Y的刻度。以同时获取Y和X
然而,这个却要巴证x+Y<=m。千万不要忘记这里!
同时,我们发现,我们还需要考虑x-Y+X的位置。因为有的位置差是Y-x,向右划刻度可能会超m,但是不代表不可以向左划。

多考虑一些,就可以AC啦!

【时间复杂度&&优化】
O(nlogn)

【数据】
2 2 1 1
0 2

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐