Codeforces 480B Long Jumps 规律题
来源:互联网 发布:淘宝首页商家怎么上去 编辑:程序博客网 时间:2024/04/29 07:27
题目链接:点击打开链接
题意:
输出n l x y
有一根直尺长度为l
上面有n个刻度。
下面n个数字是距离开头的长度(保证第一个数字是0,最后一个数字是l)
要使得 直尺中存在某2个刻度的距离为x , 某2个刻度的距离为y
要添加最少几个刻度。
问:
最少的刻度个数
输出标记的位置。
思路:
分类讨论一下。。
若本身尺子里就有x、y就输出0
若只有x 或只有y就输出一个刻度。
若2个都没有就:
1、加1个刻度ans,这个ans是距离某个刻度距离为x的,然后看一下是否有距离ans为y的刻度,若有则添加一个ans即可。
2、第1个都非法时就直接加2个刻度。
<pre name="code" class="cpp">#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <map>#include <cmath>template <class T>inline bool rd(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1;}template <class T>inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if(x>9) pt(x/10); putchar(x%10+'0');}using namespace std;typedef long long ll;const int N = 200050;vector<ll>G;bool Find(ll x){ for(int i = G.size()-2; i > 0; i--) { if(G[i] - x < 0)return false; if( G[lower_bound(G.begin(), G.end(), G[i] - x) - G.begin()] == G[i] - x) return true; } return false;}ll n, l, x, y;ll go(){ ll ans; for(int i = G.size()-2; i > 0; i--){ ans = G[i]-x; if(ans >= 0 && ((ans+y<=l&&G[lower_bound(G.begin(), G.end(), ans+y) - G.begin()] == ans+y) || (ans-y>=0&&G[lower_bound(G.begin(), G.end(), ans-y) - G.begin()] == ans-y))) return ans; ans = G[i]+x; if(ans <= l && ((ans + y <= l && G[lower_bound(G.begin(), G.end(), ans+y) - G.begin()] == ans+y) || (ans-y>=0&&G[lower_bound(G.begin(), G.end(), ans-y) - G.begin()] == ans-y))) return ans; } return -1;}void input(){ G.clear(); G.push_back(-5000000001LL); ll tmp; while(n--){ rd(tmp); G.push_back(tmp); } G.push_back(1e10);}int main() { while(cin>>n>>l>>x>>y){ input(); int havx = Find(x), havy = Find(y); if(havx + havy == 2) puts("0"); else if(havx + havy == 1) { if(havx) printf("1\n%I64d\n", y); else printf("1\n%I64d\n", x); } else { ll ans = go(); if(ans == -1) { printf("2\n%I64d %I64d\n", x, y); } else printf("1\n%I64d\n", ans); } } return 0;}
0 0
- Codeforces 480B Long Jumps 规律题
- 【CODEFORCES】 B. Long Jumps
- CodeForces 479D Long Jumps
- codeforces 479d Long Jumps
- Codeforces 479D Long Jumps(贪心+二分)
- Codeforces 479D. Long Jumps 暴力
- codeforces #479D# Long Jumps(二分upper_bound)
- codeforces 479D Long Jumps(枚举)
- Long Jumps
- Codeforces Round #274 (Div. 2) D. Long Jumps(二分)
- Codeforces Round #274 (Div. 2) d Long Jumps
- Codeforces Round #274 (Div. 2) D. Long Jumps
- Codeforces Round #274 (Div. 2)D. Long Jumps
- Codeforces 479D Long Jumps (set + 迟取法)
- Codeforces Round #274 (Div. 2)-D. Long Jumps
- Codeforces 479D Long Jumps【map标记+贪心】
- cf479D Long Jumps
- Long Jumps — 二分
- 求最长不重复子串
- va_start、va_arg、va_end使用详解
- 系统时钟
- 设计模式之原型模式
- Android加速度传感器数值的过滤
- Codeforces 480B Long Jumps 规律题
- vmware fusion设置vmnet网段的方法
- Web服务相关论文
- JSP之c:foreach
- kali linux tar.bz2 解压
- Interleaving String
- java 并发编程学习笔记(一)Executors框架
- 黑马程序员————Java_银行业务调度系统
- Linux 文件系统 (未整理 有时间整理)