Long Jumps CodeForces - 479D
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 n marks, 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.
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.
Examples
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
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.
OJ-ID:
CodeForces-479D
author:
Caution_X
date of submission:
20191109
tags:
二分,贪心
description modelling:
有一把尺子,尺子上有n个刻度A[i],问能否通过已知的刻度测出长度x和长度y?
输出需要补充的刻度点个数和对应的值
major steps to solve it:
需要补充的刻度点数只能是0,1,2
需要补充的点数为0时可以直接判断
判断能否只补充一个刻度点:①记tx=A[i]+x,表示可以在A[i]右边得出一个刻度能够测出x
再二分查找判断(tx+y)或者(tx-y)在不在已知刻度中,若在,则一个刻度点tx即可,同理,②记ty=A[i]+y,
重复类似①的操作,只要①,②有一个满足条件即可,若都不满足时:记tx=A[i]-x,表示能够在
刻度点A[i]左边找到一个刻度点测出x,记ty=A[i]-y,同理操作。若通过上述操作能够找出,则
只需要补充一个刻度点,否则,需要补充两个刻度点。
warnings:
重点在于点数1和点数2的判断,点数1需要特判
比如x=6,y=7,已知的刻度点有4,5,那么只要补充一个刻度点11即可
AC code:
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = 1e5+;
int N, L, X, Y, A[maxn]; bool judge (int u) {
if (u < || u > L) return false;
int k = lower_bound(A, A + N, u) - A;
return u == A[k];
} void solve () {
int ans = ;
for (int i = ; i < N; i++) {
if (judge(A[i] - X) || judge(A[i] + X))
ans |= ;
if (judge(A[i] - Y) || judge(A[i] + Y))
ans |= ;
} if (ans == )
printf("0\n");
else if (ans == )
printf("1\n%d\n", X);
else if (ans == )
printf("1\n%d\n", Y);
else { for (int i = ; i < N; i++) {
int tx = A[i] + X;
int ty = A[i] + Y; if (tx <= L && (judge(tx - Y) || judge(tx + Y))) {
printf("1\n%d\n", tx);
return;
} if (ty <= L && (judge(ty - X) || judge(ty + X))) {
printf("1\n%d\n", ty);
return;
}
} for (int i = ; i < N; i++) {
int tx = A[i] - X;
int ty = A[i] - Y; if (tx >= && (judge(tx - Y) || judge(tx + Y))) {
printf("1\n%d\n", tx);
return;
} if (ty >= && (judge(ty - X) || judge(ty + X))) {
printf("1\n%d\n", ty);
return;
}
}
printf("2\n%d %d\n", X, Y);
}
} int main () {
scanf("%d%d%d%d", &N, &L, &X, &Y);
for (int i = ; i < N; i++)
scanf("%d", &A[i]);
solve();
return ;
}
Long Jumps CodeForces - 479D的更多相关文章
- Codeforces 479D - Long Jumps
479D - Long Jumps, 480B - Long Jumps It , or . If we can already measure both x and y, output . Then ...
- 【Codeforces 479D】Long Jumps
[链接] 我是链接,点我呀:) [题意] 如果存在a[j]-a[i]=d 那么认为可以量出来长度d 现在给你量尺上的n个点. 问你最少要加多少个点,才能够量出来长度x和长度y [题解] 设dic1和d ...
- Discrete Centrifugal Jumps CodeForces - 1407D 单调栈+dp
题意: 给你n个数hi,你刚开始在第1个数的位置,你需要跳到第n个数的位置. 1.对于i.j(i<j) 如果满足 max(hi+1,-,hj−1)<min(hi,hj) max(hi,hj ...
- Codeforces Round #274 (Div. 1) B. Long Jumps 数学
B. Long Jumps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/ ...
- codeforces 480B B. Long Jumps(贪心)
题目链接: B. Long Jumps time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps
我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是, ...
- 【codeforces 791D】 Bear and Tree Jumps
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
- Codeforces 1500F - Cupboards Jumps(set)
Codeforces 题面传送门 & 洛谷题面传送门 nb tea!!!111 首先很显然的一件事是对于三个数 \(a,b,c\),其最大值与最小值的差就是三个数之间两两绝对值的较大值,即 \ ...
- CodeForces 771C Bear and Tree Jumps 树形DP
题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\lim ...
随机推荐
- Feign Ribbon Hystrix 三者关系 | 史上最全, 深度解析
史上最全: Feign Ribbon Hystrix 三者关系 | 深度解析 疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之 -25[ 博客园 总入口 ] 前言 疯狂创客圈(笔者尼恩创建的 ...
- C语言程序设计100例之(21):折半查找
例21 折半查找 问题描述 顺序查找是一种最简单和最基本的检索方法.其基本思想是:从检索表的一端(如表中第一个记录或最后一个记录)开始,逐个进行记录的关键字和给定值的比较.若某个记录的关键字和给定值 ...
- SpringBoot 构建 REST 服务
摘要 该文章只为了说明如何整合REST服务,并不介绍如何使用,当做笔记吧. MongoDB 以MongoDB为例 maven 依赖 <dependency> <groupId> ...
- 松软科技前端课堂:JavaScript 数值方法
Number 方法帮助您处理数值. Number 方法和属性 原始值(比如 3.14 或 2016),无法拥有属性和方法(因为它们不是对象). 但是通过 JavaScript,方法和属性也可用于原始值 ...
- Taro自定义Modal对话框组件|taro仿微信、android弹窗
基于Taro多端实践TaroPop:自定义模态框|dialog对话框|msg消息框|Toast提示 taro自定义弹出框支持编译到多端H5/小程序/ReactNative,还可以自定义弹窗类型/弹窗样 ...
- Xcode更新到10.0之后遇到的那些坑:
1.之前不在一个文件里的图片可以重新,更新之后图片不能重名了. 2.之前的双击.h.m文件不能重新开出个界面了 3. 4. ...... 未完待续
- dedecmsV5.7 arclist标签同时取出主表和附表里的数据
{dede:arclist}{/dede:arclist}标签默认取出来的是主表x_archives中的数据,如果要取出附表中的数据,需要满足两个条件: 指定channelid属性(注意:channe ...
- Data Management Technology(1) -- Introduction
1.Database concepts (1)Data & Information Information Is any kind of event that affects the stat ...
- 以太网驱动的流程浅析(二)-Ifconfig的详细代码流程【原创】
以太网驱动流程浅析(二)-ifconfig的详细代码流程 Author:张昺华 Email:920052390@qq.com Time:2019年3月23日星期六 此文也在我的个人公众号以及<L ...
- JetBrains IntelliJ IDEA 2019 for Mac(Java集成开发环境) 2019.3.1
IntelliJ IDEA 2019中文激活版已全新上线,intellij idea mac是目前编程人员最喜欢的Java集成开发环境,具备智能代码助手.代码自动提示.重构.J2EE支持.Ant.JU ...