【链接】 我是链接,点我呀:)

【题意】

如果存在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());
}
}
}

最新文章

  1. 【转】基于linux下的dm9000网卡移植全分析
  2. 简单好用的日志管理工具 Logrotate
  3. Declare Cusror of SQLServer
  4. HBase HTablePool
  5. 用户子查询,用case
  6. SSE2 Intrinsics各函数介绍[转]
  7. Java用Dijkstra算法实现地图两点的最短路径查询(Android版)
  8. *C语言有关指针的变量声明中的几个易错点
  9. Django(九)上:ModelForm操作
  10. mac openresty 源码安装 坑
  11. 在linux上安装docker
  12. css3贝塞尔曲线
  13. 使用scrapy爬取海外网学习频道
  14. vue基础篇---修改对象或数组的值,页面实时刷新
  15. js实现照片墙效果
  16. Linux获取进程执行时间
  17. 基于kettle8的web端调度监控平台
  18. (第十周)评论Beta发布
  19. mybatis由浅入深day02_2一对一查询_2.3方法二:resultMap_resultType和resultMap实现一对一查询小结
  20. 95. Unique Binary Search Trees II (Tree; DFS)

热门文章

  1. 搞定springboot项目连接远程服务器上kafka遇到的坑以及完整的例子
  2. 基于itchat实现微信群消息同步机器人
  3. select多选
  4. 页面置换算法-LRU(Least Recently Used)c++实现
  5. C#模拟百度登录并到指定网站评论回帖(四)
  6. [Python实战] 功能简单的数据查询及可视化系统
  7. Python批量下载电视剧电影--自己动手丰衣足食
  8. Python安装笔记
  9. 【Python-2.7】大小写转换函数
  10. Oracle+struts2实现用户登入并显示访问次数