【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
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.
#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
*/
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
*/
相关文章推荐
- this[] 指的是什么内容 讨论
- 对JavaScript的eval()中使用函数的进一步讨论
- 有个同学说:程序员很苦逼,而我,觉得很有意思!
- 【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest D】【模拟 讨论】Boulevard 人流行走看是否相交 beautiful code
- 【Codeforces Round 324 (Div 2)C】【构造 讨论】Marina and Vasya 构造c串使得c串与a串b串各有m个字符不同
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest A】【水题 讨论】Alex Origami Squares 矩形内置三个最大正方形
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest J】【二分答案+multiset】Journey to the “The World’
- 【HDU5565 BestCoder Round 62 (div1)C】【STL or 二分答案 or 计数排序】Clarke and baton n个人减肥m次求最后异或值
- 【Educational Codeforces Round 1A】【水题】Tricky Sum 1~n之和减去2的幂
- 【Educational Codeforces Round 1B】【字符串平移 水题】Queries on a String 字符串平移水题
- 【Educational Codeforces Round 1C】【计算几何-极角排序 atan2 long double】Nearest vectors 平面图上原点引出角度最小的两个
- 【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数
- 【Educational Codeforces Round 1E】【动态规划-多维DP】Chocolate Bar 矩形巧克力掰开吃的最小成本
- 【Codeforces Round 299 (Div 2)D】【KMP 本质是最前与最后匹配】Tavas and Malekas 长度为n的匹配串被模板串多位点覆盖的匹配串个数
- 【Codeforces Round 333 (Div 2)A 】【水题 简单进制模拟】Two Bases 不同进制数比大小
- 【Codeforces Round 333 (Div 2)B】【贪心 多指针】Approximating a Constant Range 给定数组 相邻元素波动为1 求差值不超1的最长序连续子序列
- 【Codeforces Round 333 (Div 2)C】【最短路】The Two Routes 完全图两种双向边的最小最大距离
- 【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和
- 【Codeforces Round 333 (Div 2)E】【期望DP概率做法 树状数组转前缀和】Kleofáš and the n-thlon n场比赛m个人获得总名次的期望
- 【Codeforces Testing Round 12A】【讨论 边界元素映射】Divisibility 区间范围内k倍数的数的个数