479D - Long Jumps, 480B - Long Jumps
It is easy to see that the answer is always , or . If we can already measure both x and y, output . Then try to measure both x and y by adding one more mark. If it was not successful, print two marks: one at x, other at y. So, how to check if the answer is ? Consider all existing marks. Let some mark be at r. Try to add the new mark in each of the following positions: r - x, r + x, r - y, r + y. If it become possible to measure both x and y, you have found the answer. It is easy to check this: if, for example, we are trying to add the mark at r + x, we just check if there is a mark at r + x + y or r + x - y (by a binary search, since the marks are sorted). Make sure that the adde marks are in [, L].

好久没写解题报告了。

这是一道模拟题,其实是看了Tutorial 才确定下的算法思路。

题目意思还是很简单的,在[0, L]范围内增加一个标记处,使得能测量出X,Y的值。

易得,增加标记出只有三种情况,0,1,2

0的话就是已有的标记出就可以测量出X,Y的值。

2的话就是 就算增加一个标记出也无法测量出X,Y的值。

在1这种情况下是可以分类的。

1.r -c

2.r + c

3.r - y

4.r + y

有四种可能标记处。

ex. 如果我们在(r + c)处做标记(保证合法),则判断原有序列中是否存在(r + c) - y 或者是(r + c) + y

如果存在,则得证增加这么一个(r + c)可以测量出X,Y的值。

如果还不动,画一条先端模拟一下就会懂了~

Tips: 有一点不能忘记,就是一开始算已有的标记出是否能测出X,Y的时候

不能用O(n^2)的遍历。

只需要O(n)的遍历,当然,同一数字可能同时满足测量X,Y的情况。

(查找一个值的时候,在有序序列中可用STL中的binary_search)

好久没贴代码了= =

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
using namespace std;
const int INF = 0x3f3f3f3f; int a[];
int n, l; bool Ok(int num){
if(num > && num <= l) return true;
return false;
} int main(){
int i, j, k, t, x, y;
while(EOF != scanf("%d%d%d%d",&n,&l,&x,&y)){
for(i = ; i <= n; ++i) scanf("%d",&a[i]);
bool xx = false;
bool yy = false;
for(i = ; i <= n; ++i){
if(binary_search(a, a + n + , a[i] + x)){
xx = true;
}
if(binary_search(a, a + n + , a[i] + y)){
yy = true;
}
}
if(xx && yy){
printf("0\n");
continue;
}
bool status = false;
for(i = ; i <= n; ++i){
if(Ok(a[i] - x)){
if(yy){
printf("1\n%d\n",a[i] - x);
status = true;
break;
}
if(binary_search(a, a + n + , a[i] - x - y) || binary_search(a, a + n + , a[i] - x + y)){
printf("1\n%d\n",a[i] - x);
status = true;
break;
}
}
if(Ok(a[i] + x)){
if(yy){
printf("1\n%d\n",a[i] + x);
status = true;
break;
}
if(binary_search(a, a + n + , a[i] + x - y) || binary_search(a, a + n + , a[i] + x + y)){
printf("1\n%d\n",a[i] + x);
status = true;
break;
}
}
if(Ok(a[i] - y)){
if(xx){
printf("1\n%d\n",a[i] - y);
status = true;
break;
}
if(binary_search(a, a + n + , a[i] - y - x) || binary_search(a, a + n + , a[i] - y - x)){
printf("1\n%d\n",a[i] - y);
status = true;
break;
}
}
if(Ok(a[i] + y)){
if(xx){
printf("1\n%d\n",a[i] + y);
status = true;
break;
}
if(binary_search(a, a + n + , a[i] + y - x) || binary_search(a, a + n + , a[i] + y + x)){
printf("1\n%d\n",a[i] + y);
status = true;
break;
}
}
}
if(!status){
printf("2\n%d %d\n",x,y);
}
}
return ;
}

Codeforces 479D - Long Jumps的更多相关文章

  1. Long Jumps CodeForces - 479D

    E - Long Jumps CodeForces - 479D Valery is a PE teacher at a school in Berland. Soon the students ar ...

  2. 【Codeforces 479D】Long Jumps

    [链接] 我是链接,点我呀:) [题意] 如果存在a[j]-a[i]=d 那么认为可以量出来长度d 现在给你量尺上的n个点. 问你最少要加多少个点,才能够量出来长度x和长度y [题解] 设dic1和d ...

  3. Codeforces 1500F - Cupboards Jumps(set)

    Codeforces 题面传送门 & 洛谷题面传送门 nb tea!!!111 首先很显然的一件事是对于三个数 \(a,b,c\),其最大值与最小值的差就是三个数之间两两绝对值的较大值,即 \ ...

  4. Codeforces 480B Long Jumps 规律题

    题目链接:点击打开链接 题意: 输出n l x y 有一根直尺长度为l 上面有n个刻度. 以下n个数字是距离开头的长度(保证第一个数字是0,最后一个数字是l) 要使得 直尺中存在某2个刻度的距离为x ...

  5. 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/ ...

  6. codeforces 480B B. Long Jumps(贪心)

    题目链接: B. Long Jumps time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. 【树形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的整数倍,还要加上多少. 于是, ...

  8. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  9. CodeForces 771C Bear and Tree Jumps 树形DP

    题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\lim ...

随机推荐

  1. Object-c Block的使用及说明

    Object-c 中的block就好像一段C函数般,由函数名,有返回值,有参数,由函数体等 1.简单的block ^(int A ,int B) { int C=A*B; return C; }; 上 ...

  2. 自己动手写List集合(C#)

    平时经常使用微软的List集合,觉得理所应当,这阵子突然意识到学编程学这么久,总不能只生存在某个平台某种语言下面.我觉得要跳出这个框,而数据结构是经常用到的,所以呢,作为一个有志向的程序员应该学会它. ...

  3. VICC国际标准ISO15693下载

    疏耦合卡(VICC)国际标准ISO15693-1点击下载 疏耦合卡(VICC)国际标准ISO15693-2点击下载 疏耦合卡(VICC)国际标准ISO15693-3点击下载

  4. PCB抄板评估需要关注的因素

    减少PCB抄板的反复是可能的,但这依赖于抄板前期工作的完成情况.多数时候,越是到产品抄板的后期越容易发现问题,更为痛苦的是要针对发现的问题进行更改.然而,尽管许多人都清楚这个经验法则,但实际情况却是另 ...

  5. java学习之即时通信项目实战

     项目总结:这次项目主要是根据视频来的,结果跟到一半感觉跟不上,慢慢自己有了自己的想法,决定自己先不看学习视频,自己先试着写. 总结写前面,算是写的第一个项目吧.项目中遇到几点问题,首先Scoket对 ...

  6. SSH框架的简单学习—Structs学习

    一:struts部分 1.打开Myeclipse,创建一个web project,项目名称为SSHDemo. 2.在web的lib下粘贴struts2-blank.war解压后WEB-INF\lib下 ...

  7. PyQt写的浏览单web页面的browser - 开源中国社区

    PyQt写的浏览单web页面的browser - 开源中国社区 PyQt写的浏览单web页面的browser

  8. Warning: Unable to send packet: Error with PF_PACKET send() [11]: Message too long (errno = 90)

    今天在使用tcpreplay重放流量时,发现有的数据包没有发送成功: Warning: Unable to send packet: Error with PF_PACKET send() [215] ...

  9. Spark学习体系

    底理解Spark,能够分为以下几个层次. 1 Spark基础篇 1.1 Spark生态和安装部署 在安装过程中,理解其基本操作步骤. 安装部署 Spark安装简单介绍 Spark的源代码编译 Spar ...

  10. 用nodejs安装hexo,将hexo部署到github

    跌跌撞撞写这篇博文,希望下一篇可以好点 运行环境:最新版本的nodejs + git 安装好nodejs 和 git ,注册好github账号,新建仓库****.github.io(****为gith ...