codeforces #479D# Long Jumps(二分upper_bound)
2015-01-02 21:42
183 查看
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
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.
解题思路:这题意思就是给一把尺子上面给定一些刻度,要你从这把尺子上取出x,y两个长度,问你至少要添加几个点才能够实现。
毫无疑问最多是两个点,我开了三个数组,所以内存比较大了些。我是讲 ai +x 和ai-x存到数组里面,然后到ai数组中看看能不能找到对应的点,有就不表示能取到,y也是同理的。这题比较不容易想到一个点就是可能取到一个点,刚好让原来不能测量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.
解题思路:这题意思就是给一把尺子上面给定一些刻度,要你从这把尺子上取出x,y两个长度,问你至少要添加几个点才能够实现。
毫无疑问最多是两个点,我开了三个数组,所以内存比较大了些。我是讲 ai +x 和ai-x存到数组里面,然后到ai数组中看看能不能找到对应的点,有就不表示能取到,y也是同理的。这题比较不容易想到一个点就是可能取到一个点,刚好让原来不能测量x,y的尺子能够测量两个数据了。这个要小心了。
/********************************/ /*Problem: codeforces #483B# */ /*User: shinelin */ /*Memory: 3400 KB */ /*Time: 62 ms */ /*Language: GNU C++ */ /********************************/ #include <cstdio> #include <iostream> #include <cstdlib> #include <ctime> #include <cctype> #include <cstring> #include <string> #include <list> #include <map> #include <queue> #include <deque> #include <stack> #include <vector> #include <set> #include <algorithm> #include <cmath> using namespace std; #define INF 0x7fffffff #define LL long long int n, l, x, y, d; vector<int> xNeed, yNeed; vector<int> point; vector<int> ans; bool getx = false, gety = false; int cnt = 2; bool if_Find(int d, vector<int>& v) { int k = upper_bound(v.begin(), v.end(), d) - v.begin(); if(k > 0 && v[k-1] == d) { return true; } return false; } int main() { scanf("%d%d%d%d", &n, &l, &x, &y); for(int i = 0; i < n; i ++) { scanf("%d", &d); point.push_back(d); if(d-x >= 0) xNeed.push_back(d-x); if(d-y >= 0) yNeed.push_back(d-y); if(d+x <= l) xNeed.push_back(d+x); if(d+y <= l) yNeed.push_back(d+y); } sort(xNeed.begin(), xNeed.end()); sort(yNeed.begin(), yNeed.end()); sort(point.begin(), point.end()); for(int i = 0; i < n; i ++) { if(!getx && if_Find(point[i], xNeed)) { getx = true; cnt --; } if(!gety && if_Find(point[i], yNeed)) { gety = true; cnt --; } } if(cnt == 2) { for(int i = 0; i < xNeed.size(); i ++) { if(if_Find(xNeed[i], yNeed)) { printf("1\n%d\n", xNeed[i]); return 0; } } } printf("%d\n", cnt); if(cnt == 0) return 0; if(!getx) ans.push_back(x); if(!gety) ans.push_back(y); int k = 0; while(k < ans.size() - 1) { printf("%d ", ans[k++]); } printf("%d\n", ans[k]); return 0; }
相关文章推荐
- Leetcode 34 Search for a Range (二分搜索 lower_bound和upper_bound)
- 二分--lower_bound()函数,upper_bound()函数
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- 【HDU】 5101 Select 二分搜索 upper_bound
- 二分查找,lower_bound,upper_bound
- STL 二分查找 upper_bound和lower_bound用法
- hdu 5178(二分-lower_bound,upper_bound)
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- C中的二分函数:upper_bound()与lower_bound()
- 关于二分(upper_bound,lower_bound)
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- 二分查找及lower_bound与upper_bound
- STL中的二分查找--lower_bound/upper_bound/binary_search
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
- STL中二分查找相关算法(binary_search, lower_bound, upper_bound, equal_range)
- 二分lower_bound()与upper_bound()的运用
- UVa10474 Where is the Marble ? 有序数组二分找值,lower_bound / upper_bound
- Gym - 101257G 24-二分和upper_bound的应用
- 二分查找确定lower_bound和upper_bound