【CODEFORCES】 B. Long Jumps
2015-08-06 15:51
495 查看
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.
题解:拿到这题首先可以确定答案不是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 a[i]+x=a[j]-y a[i]-x=a[j]+y a[i]-x=a[j]-y x+y=a[i] x=a[i] y=a[i]这样7种情况,具体就不做说明了,大概算一下都懂),如果这些情况全不满足,那么答案就是2,一个x一个y。
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.
题解:拿到这题首先可以确定答案不是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 a[i]+x=a[j]-y a[i]-x=a[j]+y a[i]-x=a[j]-y x+y=a[i] x=a[i] y=a[i]这样7种情况,具体就不做说明了,大概算一下都懂),如果这些情况全不满足,那么答案就是2,一个x一个y。
#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\n"); return 0; } for (int i=1;i<=n;i++) if (bse(a,1,n,x-y+a[i]) && x+a[i]<=l) { printf("1\n%d\n",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\n%d\n",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\n%d\n",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\n%d\n",a[i]-x); return 0; } if (p) printf("1\n%d\n",y); else if (q) printf("1\n%d\n",x); else { for (int i=1;i<=n;i++) if (a[i]==x+y) {p=1; break;} if (p) printf("1\n%d\n",x); else printf("2\n%d %d\n",x,y); } return 0; }
相关文章推荐
- 线程安全与并发安全探究(四)-java集合框架
- hdu 1806 Frequent values(给定一个非降序数组,求任意区间内出现次数最多的数的次数)
- Vert.x-Web的讲解和使用(三)
- 手把手教你用Git
- EL表达式
- Factoring Large Numbers
- java代码--汉诺塔
- HDU 1272 小希的迷宫
- SVG绘图
- linux rar
- rsync 删除海量文件
- java Method用法
- spring和java国际化资源文件加载顺序
- WSAD 如何修改JDK以使用jdk1.4
- Android 上实现像微信一样的用Fragment来实现的Tab切页效果 提供源码下载
- JS异步处理 setTimeout / setInterval 比较
- js滚动条
- 【黑马程序员】Java学习笔记--异常
- 一张6亿条数据表引发的事故
- nginx进程间的通信机制源码分析(二)-------原子操作、自旋锁、文件锁