Codeforces 479D - Long Jumps
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的更多相关文章
- Long Jumps CodeForces - 479D
E - Long Jumps CodeForces - 479D Valery is a PE teacher at a school in Berland. Soon the students ar ...
- 【Codeforces 479D】Long Jumps
[链接] 我是链接,点我呀:) [题意] 如果存在a[j]-a[i]=d 那么认为可以量出来长度d 现在给你量尺上的n个点. 问你最少要加多少个点,才能够量出来长度x和长度y [题解] 设dic1和d ...
- Codeforces 1500F - Cupboards Jumps(set)
Codeforces 题面传送门 & 洛谷题面传送门 nb tea!!!111 首先很显然的一件事是对于三个数 \(a,b,c\),其最大值与最小值的差就是三个数之间两两绝对值的较大值,即 \ ...
- Codeforces 480B Long Jumps 规律题
题目链接:点击打开链接 题意: 输出n l x y 有一根直尺长度为l 上面有n个刻度. 以下n个数字是距离开头的长度(保证第一个数字是0,最后一个数字是l) 要使得 直尺中存在某2个刻度的距离为x ...
- 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 771C Bear and Tree Jumps 树形DP
题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\lim ...
随机推荐
- python函数callable
callable(object) 中文说明:检查对象object是否可调用.如果返回True,object仍然可能调用失败:但如果返回False,调用对象ojbect绝对不会成功. 注意:类或函数是可 ...
- python mysql多条插入
程序的目的是把文本里面的数据存储到数据库中,原来的思路是读一条,插入一条,结果就是时间长的不得了...18万条的数据,真是慢. 后来的想法是把所有的记录都读到一个list里,结果是mysql奔溃go ...
- sql语法复习:增删查改,各种数据库对象创建和函数使用
推荐工具:机子配置较低的话,可以装Gsql这个工具获得sql执行环境(可作为手册查看内置数据类型 函数和存储过程等) --之前数据库的东西接触不多,虽然基本的语法是了解,但不是很熟悉--最近项目一直在 ...
- Php 使用 fsockopen发送http请求
<?php function HTTP_Post($URL,$data, $referrer="") { // parsing the given URL $URL_Info ...
- ActionScript3游戏中的图像编程(连载二十四)
总文件夹:http://blog.csdn.net/iloveas2014/article/details/38304477 2.1.1 投影样式的制作 点击左側列表的"投影"系列 ...
- Oracle PL/SQL 游标
在PL/SQL块中执行SELECT.INSERT.DELETE和UPDATE语句时,ORACLE会在内存中为其分配上下文区(Context Area),即缓冲区.游标是指向该区的一个指针,或是命名一个 ...
- 【转】android开发中关于模拟器emulation的常见问题
[转]android开发中关于模拟器emulation的常见问题 Trouble: 无法启动android模拟器,提示 XDM authorization key matches an existin ...
- 语句分类及if语句
一.语句分类: 1.顺序语句2.分支语句 if语句.switch语句 3.循环语句 (1)初始条件 (2)循环条件 (3)循环体 (4)状态改变 二.if语句: 1.if语句4中情况: if(条件){ ...
- uml笔记
把进度放在好了: 活动图与业务流程 对业务流程支持的主要图形就是活动图,活动图的主要目的在陈述活动与活动之间流程控制的转移.
- 解决虚拟内存不够导致Eclipse is not responding
安装目录下eclipse.ini中: 修改参数至必要大小. e.g. -vmargs-Djava.net.preferIPv4Stack=true-Dosgi.requiredJavaVersion= ...