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 ...
随机推荐
- windows 7 里面的iis在哪里
我的电脑------控制面板-----卸载程序------打开或关闭WINDOWS功能-----INTERNET信息服务------WEB管理工具下就能找到 给你找图你看看 向左转|向右转
- android学习----overridePendingTransition
1 Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画. 它包括两个部分:一部分是第一个activity退出时的动画:另外一部分时第二个activity进入时 ...
- CI 模板解析器类
模板解析器类可以解析你的视图文件中的伪变量.它可以解析简单的变量或者以变量作为标签的结构.如果你以前没有用过模板引擎,那么伪变量如下所示: <html><head><ti ...
- <Win32_16>来看看标准菜单和右键菜单的玩法
日常应用中,菜单主要分为两种:(1) 标准菜单(处于应用程序菜单栏处的菜单) (2)右键快捷菜单 几乎你所见过或使用过的软件中,都有它俩儿 为应用程序添加它们的基本步骤: (1)用代码或者IDE ...
- 如何调试MFC中的内存泄漏
转载地址:http://www.cnitblog.com/martin/archive/2006/04/21/9460.html 首先,应该是MFC报告我们发现内存泄漏.注意:要多运行几次,以确定输出 ...
- Opencv 学习资料集合(更新中。。。)
基础学习笔记之opencv(24):imwrite函数的使用 tornadomeet 2012-12-26 16:36 阅读:13258 评论:9 基础学习笔记之opencv(23):OpenCV坐标 ...
- Foundation Sorting: Quicksort
/* Quick Sorting. * Implementation history:. * 2013-09-15, Mars Fu, first version. */ /* [Quicksort ...
- .net 常用方法
1.String数组转换成Int数组 string[] strArr = "a,b,c".Split(','); int[] intArr = Array.ConvertAll& ...
- 比较和排序 IComparable And IComparer
1.List<Student>默认排序 为类创建默认排序实现IComparable,此代码的实现为年龄升序 using System; using System.Collections.G ...
- 搭建zend framework1开发环境
1.和常规开发大致相同,首先下载zend framework1,下载地址如下 http://www.zendframework.com/downloads/latest 挑选其中一个下载,我下载的是f ...