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. yii教程

    http://www.yiichina.com/doc 官网是很好的参考文档

  2. JS性能

    获取以下属性  会等待对应元素渲染完成  才继续执行 * offsetTop, offsetLeft, offsetWidth, offsetHeight* scrollTop, scrollLeft ...

  3. docker4dotnet

    docker4dotnet #1 – 前世今生 & 世界你好   作为一名.NET Developer,这几年看着docker的流行实在是有些眼馋.可惜的是,Docker是基于Linux环境的 ...

  4. HeapAnalyzer

    https://www.ibm.com/developerworks/community/groups/service/html/communityview?communityUuid=4544baf ...

  5. Linux的默认编码可以通过export LC_ALL=zh_CN.GBK来修改

    http://www.cnblogs.com/malecrab/p/5300486.html

  6. CERC 2013 Magical GCD

    题目大意如下:给定一个序列,每个序列有值xi,现给定t个数列,对于每个长n的数列,求一段[l,r]使 [r-l+1]*gcd(l,r)最大,gcd(l,r)指的是该连续区间的最大公约数. 不难想到n^ ...

  7. ActionScript3游戏中的图像编程(连载十七)

    总文件夹:http://blog.csdn.net/iloveas2014/article/details/38304477 1.3.3 HSB与RGB之间的互转公式及HSL和HSV对色彩属性理解的异 ...

  8. jQuery也能舞出绚丽的界面(完结篇)

    ThematicMap又增加了两种Chart类型,现在总算是齐全了,效果也出来了,与大家分享一下: 1.MultiSelect选择界面: 颜色框是可以选择颜色的: 2.生成的饼图效果: 3.生成的柱状 ...

  9. 关于SOQL(一)

    SOQL 是Salesforce中的查询语言,他的全称是Salesforce Object Query Language. 从字面上就能够看出,这个语言是一种基于对象的查询语言. 在Salesforc ...

  10. Tableau Server 8.0 升级到 8.3 过程记录

    一.使用账号(管理员权限),安装文件复制到服务器 二.检查维护状态 如果维护状态过期,更新到新版本会变成未授权. 先进Manage Product Keys刷新一下维护日期(其实不刷新也无所谓.到时候 ...