codeforces 480B B. Long Jumps(贪心)
題目鏈接:
B. Long Jumps
time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outputValery 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.
InputThe 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.
OutputIn 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.
Examples input 3 250 185 2300 185 250 output 1
230 input 4 250 185 230
0 20 185 250 output 0 input 2 300 185 230
0 300 output 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; }
轉載于:https://www.cnblogs.com/zhangchengc919/p/5843903.html
總結
以上是生活随笔為你收集整理的codeforces 480B B. Long Jumps(贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多行字符串拼接
- 下一篇: Asp.net Core 使用Redis