【Codeforces 479D】Long Jumps
【链接】 我是链接,点我呀:)
【题意】
如果存在a[j]-a[i]=d
那么认为可以量出来长度d
现在给你量尺上的n个点.
问你最少要加多少个点,才能够量出来长度x和长度y
【题解】
设dic1和dic2分别为
能量出长度x和长度y需要添加的点(所有能利用某个a[i]量出来长度为x或y的点)
(输入a[i]的话,就把a[i]-x和a[i]+x加入dic1,把a[i]-y和a[i]+y加入dic2
(一开始我只加了a[i]-x......傻逼了)
如果一开始就能量出来x和y(不用这两个集合里面的元素(代码里面用的是map)
则输出0
否则
如果长度x和长度y都一开始不能量出来
那么需要添加点
添加几个点呢?
添加两个点是肯定可以的(x,y)
但是我们可以想办法让他变得更优.
怎么办呢?
我们可以遍历dic1中的所有数字tmp
如果在dic2中也有出现的话
那就说明这个数字tmp可以让x和y都能量出来.
则输出tmp就可以了.
就这点比较特殊
其他的都比较容易想 只需输出x或者y就行
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = 50000;
static class Task{
int n,l,x,y;
boolean ok1,ok2;
Map<Integer,Integer> dic,dic1,dic2;
public void solve(InputReader in,PrintWriter out) {
dic = new HashMap<Integer,Integer>();
dic1 = new HashMap<Integer,Integer>();
dic2 = new HashMap<Integer,Integer>();
ok1 = false;ok2 = false;
n = in.nextInt();l = in.nextInt();
x = in.nextInt();y = in.nextInt();
for (int i = 1;i <= n;i++) {
int ai;
ai = in.nextInt();
if (dic.containsKey(ai-x)) ok1 = true;
if (dic.containsKey(ai-y)) ok2 = true;
dic1.put(ai-x, 1);
dic1.put(ai+x, 1);
dic2.put(ai-y, 1);
dic2.put(ai+y, 1);
dic.put(ai, 1);
}
if (ok1 && ok2) {
out.println(0);
}else if (!ok1 && !ok2) {
for (Map.Entry<Integer,Integer> it:dic1.entrySet()) {
int x = it.getKey();
if (x<0) continue;
if (x>l) continue;
if (dic2.containsKey(x)) {
out.println(1);
out.println(x);
return;
}
}
out.println(2);
out.println(x+" "+y);
}else {
out.println(1);
if (!ok1) {
out.println(x);
}else {
out.println(y);
}
}
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 479D】Long Jumps的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 791D】 Bear and Tree Jumps
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
- 【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- centos 重装docker
docker应该是root用户来使用,因为他连接了底层!!!以下操作默认是root用户来操作的 停止所有正在运行的容器: docker stop $(docker ps -a -q) 删除所有的容器c ...
- Spring boot 分环境部署
一.如果配置文件为:application.properties时 1.application.properties用于填些公共文件 以下为不同环境的配置文件需要单独配置 application-de ...
- 数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换
参考来源:http://blog.csdn.net/qq924862077/article/details/54286976?utm_source=gold_browser_extension Req ...
- 最新的 xp sp3序列号(绝对可通过正版验证)-xp序列号
windows xp激活方法一:使用windows xp激活码 以下xp系统激活码都是可用的 MRX3F-47B9T-2487J-KWKMF-RPWBY(工行版) 可用(强推此号) QC986-27D ...
- 237 Delete Node in a Linked List 删除链表的结点
编写一个函数,在给定单链表一个结点(非尾结点)的情况下,删除该结点. 假设该链表为1 -> 2 -> 3 -> 4 并且给定你链表中第三个值为3的节点,在调用你的函数后,该链表应变为 ...
- jQuery学习笔记(2)-选择器的使用
一.选择器是什么 有了jQuery的选择器,我们几乎可以获取页面上任意一个或一组对象 二.Dom对象和jQuery包装集 1.Dom对象 JavaScript中获取Dom对象的方式 <div i ...
- Scala-基础-数组(1)
import junit.framework.TestCase import scala.collection.mutable.ArrayBuffer; //数组(1) //知识点-定义数组,变长数组 ...
- Python :用两个栈实现队列
转自:http://blog.csdn.net/Lynette_bb/article/details/75092745 牛客网上的剑指 offer的在线编程: 题目描述 用两个栈来实现一个队列,完成队 ...
- 十年后我不会log,还是活的很好啊
混迹于互联网也一两年了,出于喜爱与生活压力依然会从事很久.迟迟不写博客,大概是觉得知识与经验积累在笔记上时不时看看就好了,而实际情况是笔记很少翻,遇到问题搜博客和百度依然是首选,故开通博客记录自己工作 ...
- Redis系列(四)--持久化
持久化就是将数据的更新异步的保存到磁盘中 持久化方式: 1.快照:某个时间点数据的备份 MySQL dump.Redis RDB 2.写日志:MySQL BinLog.HBASE Hlog.Redis ...