codeforces Round #274(div2) D解题报告
2014-10-20 12:13
507 查看
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 aidenotes
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.
由题意可知,标记增加量可能为:0, 1, 2;
为0个的时候: 则x和y都可以在尺子上找到,即a[i]+x和a[i]+y均可在尺子上找到,我们可以采用二分搜索。复杂度为O(nlogn);
为1个的时候: 第一种情况:x或y其中一个可以在尺子上找到;
第二种情况:可以找到组合点,即 a[i]+x+y, a[i]-x-y, a[i]+x-y, a[i]-x+y,可以合并为a[i]+x+y, a[i]-x+y 。这里需要注意的是,中转点可能越界,中转点可能 有2个,一个是a[i]-x,一个是a[i]+y。只需要其中一个符合就勾勒。
为2个的时候:上述情况都找不到,则只能是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 aidenotes
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.
题目大意:
给出一把尺子,总长度为l,其中有n个标记,其中第一个标记为0, 第n个标记为l。要求量出x和y,如若不能量出,可以在上面增加标记。要求增加的标记最少。解法:
这题,我们必然需要查询某个数,这里我们可以让ai单调(题目已经预设是单调的),然后采用二分来查询,这样,每次查询只需要O(logn)的时间复杂度,这是本题的基本的核心。由题意可知,标记增加量可能为:0, 1, 2;
为0个的时候: 则x和y都可以在尺子上找到,即a[i]+x和a[i]+y均可在尺子上找到,我们可以采用二分搜索。复杂度为O(nlogn);
为1个的时候: 第一种情况:x或y其中一个可以在尺子上找到;
第二种情况:可以找到组合点,即 a[i]+x+y, a[i]-x-y, a[i]+x-y, a[i]-x+y,可以合并为a[i]+x+y, a[i]-x+y 。这里需要注意的是,中转点可能越界,中转点可能 有2个,一个是a[i]-x,一个是a[i]+y。只需要其中一个符合就勾勒。
为2个的时候:上述情况都找不到,则只能是2个了,即一个是x,一个是y。
代码:
#include <cstdio> #define N_max 123456 int n, l, x, y; int a[N_max]; void init() { scanf("%d%d%d%d", &n, &l, &x, &y); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); } int find(int key) { int l = 1; int r = n; while (l <= r) { int midn = (l+r)/2; if (key < a[midn]) r = midn-1; else if (key > a[midn]) l = midn+1; else return midn; } return 0; } bool check_0() { bool flag = false; for (int i = 1; i <= n; i++) if (find(a[i]+x)) { flag = true; break; } if (!flag) return false; flag = false; for (int i = 1; i <= n; i++) if (find(a[i]+y)) { flag++; break; } if (!flag) return false; return true; } bool check_1() { for (int i = 1; i <= n; i++) { if (find(a[i]+x)) { printf("1\n%d\n", y); return true; } if (find(a[i]+y)) { printf("1\n%d\n", x); return true; } } for (int i = 1; i <= n; i++) { if (find(a[i]+x+y)) { printf("1\n%d\n", a[i]+x); return true; } if (find(a[i]+y-x)) { if (a[i]-x >= 0 && a[i]-x <= l) { printf("1\n%d\n", a[i]-x); return true; } else if (a[i]+y >= 0 && a[i]+y <= l) { printf("1\n%d\n", a[i]+y); return true; } } } return false; } void solve() { if (check_0()) printf("0\n"); else if (check_1()) return; else printf("2\n%d %d\n", x, y); } int main() { init(); solve(); }
相关文章推荐
- Codeforces Round #274 (Div. 2) 解题报告
- Codeforces Round #274 (Div. 2) 解题报告 (C D E)
- Codeforces Round #191 (Div. 2) 解题报告
- Codeforces Round #188 (Div. 2) 解题报告 //缺E
- Codeforces Round #232 (Div. 1) A 解题报告
- Codeforces Round #191 (Div. 2) 解题报告
- Codeforces Round #240 (Div. 1) 前3题 解题报告
- Codeforces Round #283 (Div. 1)解题报告A.B.C.
- Codeforces Round #281 (Div. 2) 解题报告 A.B.C.D.
- Codeforces Round #277.5 (Div. 2) 解题报告
- Codeforces Round #278 (Div. 1) 解题报告 A.B.
- codeforces Round #241(div2) D解题报告
- Codeforces Round #226 (Div. 2) 解题报告
- Codeforces Round #345 (Div. 2) 解题报告
- Codeforces Round #279 (Div. 2) 解题报告(A B C D)
- Codeforces Round #192 (Div. 2) 解题报告 //缺E
- Codeforces Round #299 Div2 解题报告
- Codeforces Round #306 (Div. 2) D.E. 解题报告
- Codeforces Round #291 (Div. 2) 解题报告 (A B C D)
- codeforces round 200 div2解题报告