CodeForces 479D Long Jumps
2014-10-19 21:34
489 查看
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
output
input
output
input
output
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.
CF每道题都是如此的经典,这道题注意的点主要都是细节,相信二分大家都已经想到了,不过要注意它只能大的减小的。所以对于已经有的mask,是不能相加得到新mark的,二分就是从最大的已有mask开始,若它大于x(y相同),就从小于等于这个mask的mask中寻找是否有这个差值(a[i]-x),第一步完成。
然后就是第二部,一个数字已有和两个数字都有的情况不说了。若x,y都不能通过现有mask得到,则先循环一遍欲一步得到x所需要的所有数字,注意这边不仅要判断a[i]-x,也要判断a[i]+x,更要注意的是a[i]-x>0 a[i]+x<=l ,这是个trick,若非如此,那它也不需要告诉你l了。然后循环一遍y,如果有相同的数字,那就选这个数字了,如果没有,那就随便输出个可行解。存储和查询也要用二分,或者map等等。
代码:
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.
CF每道题都是如此的经典,这道题注意的点主要都是细节,相信二分大家都已经想到了,不过要注意它只能大的减小的。所以对于已经有的mask,是不能相加得到新mark的,二分就是从最大的已有mask开始,若它大于x(y相同),就从小于等于这个mask的mask中寻找是否有这个差值(a[i]-x),第一步完成。
然后就是第二部,一个数字已有和两个数字都有的情况不说了。若x,y都不能通过现有mask得到,则先循环一遍欲一步得到x所需要的所有数字,注意这边不仅要判断a[i]-x,也要判断a[i]+x,更要注意的是a[i]-x>0 a[i]+x<=l ,这是个trick,若非如此,那它也不需要告诉你l了。然后循环一遍y,如果有相同的数字,那就选这个数字了,如果没有,那就随便输出个可行解。存储和查询也要用二分,或者map等等。
代码:
#include<iostream> #include<cstdio> #include<cstdlib> #include<ctime> #include<string> #include<cstring> #include<algorithm> #include<fstream> #include<queue> #include<stack> #include<vector> #include<cmath> #include<iomanip> #include<map> #define rep(i,n) for(i=1;i<=n;i++) #define MM(a,t) memset(a,t,sizeof(a)) #define INF 1e9 typedef long long ll; #define mod 1000000007 using namespace std; int n,l,x,y; int a[100020]; map<int,bool> mp; bool f1,f2; bool se(int ii,int num){ int l,r,mid; l=1; r=ii; while(l<=r){ mid=(l+r)/2; if(num>=a[mid]){ if(num==a[mid]) return true; l=mid+1; } else r=mid-1; } return false; } int main() { int i,j; while(scanf("%d%d%d%d",&n,&l,&x,&y)!=EOF){ rep(i,n) scanf("%d",&a[i]); f1=false; f2=false; for(i=n;i>=1;i--){ if(!f1 && a[i]>=x) f1=se(i,a[i]-x); if(!f2 && a[i]>=y) f2=se(i,a[i]-y); } if(f1 && f2) printf("0\n"); else if(f1 || f2){ if(!f1){ printf("1\n"); printf("%d\n",x); } else{ printf("1\n"); printf("%d\n",y); } } else{ int zhi=-1; mp.clear(); rep(i,n){ if(a[i]>=x){ mp[a[i]-x]=true; } if(a[i]+x<=l){ mp[a[i]+x]=true; } } rep(i,n){ if(a[i]>=y){ if(mp[a[i]-y]){ zhi=a[i]-y; break; } } if(a[i]+y<=l){ if(mp[a[i]+y]){ zhi=a[i]+y; break; } } } if(zhi==-1){ printf("2\n"); printf("%d %d\n",x,y); } else{ printf("1\n"); printf("%d\n",zhi); } } } return 0; }
相关文章推荐
- Codeforces 479D - Long Jumps
- Codeforces 479D Long Jumps (set + 迟取法)
- 【CODEFORCES】 B. Long Jumps
- codeforces 480B B. Long Jumps(贪心)
- codeforces 455A Boredom dp
- CodeForces 44B Cola
- Codeforces 335B Palindrome
- CodeForces 141C Queue
- Codeforces 528A Glass Carving STL模拟
- CodeForces - 651A Joysticks ( 不难 但有坑 )
- Codeforces 455B A Lot of Games(博弈)
- Codeforces 598D:Igor In the Museum
- CodeForces 105A
- codeforces 722C. Destroying Array
- CodeForces 402D Upgrading Array (数学+DP)
- CodeForces 890B Vald and Cafes
- CodeForces 56E-Find the Path(技巧)
- Codeforces 596C Wilbur and Points 【贪心 + sort】
- codeforces 361 E - Mike and Geometry Problem
- 【Codeforces 141CQueue】+ 数组构造