codeforces 480B B. Long Jumps(贪心)
题目链接:
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.
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.
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.
3 250 185 230
0 185 250
1
230
4 250 185 230
0 20 185 250
0
2 300 185 230
0 300
2
185 230
题意:
现在有个长为l的尺子,上面有n个标记,现在要量两个长度x和y,问最小加几个标记才可以;分别加在哪;
思路:
最多要加两个标记,如果有标记恰好间隔x,y那么就可以直接量了,如果有一个可以量,那么再加另外一个就好了,要是两个都不能量,但加一个可以量两个,那么加一个就好,否则加两个,判断就用map;
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=1e6+20; const int maxn=5e3+10; const double eps=1e-12; int n; LL l,x,y,a[N]; map<LL,int>mp; int check1(LL d) { for(int i=1;i<=n;i++) { LL temp=a[i]+d; if(mp[temp])return 1; } return 0; } int check2() { for(int i=1;i<=n;i++) { LL temp=a[i]+x+y; if(mp[temp]) { cout<<"1\n"; cout<<a[i]+x<<endl; return 0; } } LL leng=y-x; for(int i=1;i<=n;i++) { LL temp=a[i]+leng; if(mp[temp]) { if(temp+x<=l){cout<<"1\n";cout<<temp+x<<endl;return 0;} if(a[i]-x>=0){cout<<"1\n";cout<<a[i]-x<<endl;return 0;} } } cout<<"2"<<endl; cout<<x<<" "<<y<<endl; return 0; } int main() { read(n);read(l);read(x);read(y); For(i,1,n)read(a[i]),mp[a[i]]=1; int fx=check1(x),fy=check1(y); if(fx&&fy)cout<<"0\n"; else if(fx||fy) { if(fx)cout<<"1\n"<<y<<endl; else cout<<"1\n"<<x<<endl; } else check2(); return 0; }