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

【题意】

如果存在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就行

【代码】

  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4. static InputReader in;
  5. static PrintWriter out;
  6. public static void main(String[] args) throws IOException{
  7. //InputStream ins = new FileInputStream("E:\\rush.txt");
  8. InputStream ins = System.in;
  9. in = new InputReader(ins);
  10. out = new PrintWriter(System.out);
  11. //code start from here
  12. new Task().solve(in, out);
  13. out.close();
  14. }
  15. static int N = 50000;
  16. static class Task{
  17. int n,l,x,y;
  18. boolean ok1,ok2;
  19. Map<Integer,Integer> dic,dic1,dic2;
  20. public void solve(InputReader in,PrintWriter out) {
  21. dic = new HashMap<Integer,Integer>();
  22. dic1 = new HashMap<Integer,Integer>();
  23. dic2 = new HashMap<Integer,Integer>();
  24. ok1 = false;ok2 = false;
  25. n = in.nextInt();l = in.nextInt();
  26. x = in.nextInt();y = in.nextInt();
  27. for (int i = 1;i <= n;i++) {
  28. int ai;
  29. ai = in.nextInt();
  30. if (dic.containsKey(ai-x)) ok1 = true;
  31. if (dic.containsKey(ai-y)) ok2 = true;
  32. dic1.put(ai-x, 1);
  33. dic1.put(ai+x, 1);
  34. dic2.put(ai-y, 1);
  35. dic2.put(ai+y, 1);
  36. dic.put(ai, 1);
  37. }
  38. if (ok1 && ok2) {
  39. out.println(0);
  40. }else if (!ok1 && !ok2) {
  41. for (Map.Entry<Integer,Integer> it:dic1.entrySet()) {
  42. int x = it.getKey();
  43. if (x<0) continue;
  44. if (x>l) continue;
  45. if (dic2.containsKey(x)) {
  46. out.println(1);
  47. out.println(x);
  48. return;
  49. }
  50. }
  51. out.println(2);
  52. out.println(x+" "+y);
  53. }else {
  54. out.println(1);
  55. if (!ok1) {
  56. out.println(x);
  57. }else {
  58. out.println(y);
  59. }
  60. }
  61. }
  62. }
  63. static class InputReader{
  64. public BufferedReader br;
  65. public StringTokenizer tokenizer;
  66. public InputReader(InputStream ins) {
  67. br = new BufferedReader(new InputStreamReader(ins));
  68. tokenizer = null;
  69. }
  70. public String next(){
  71. while (tokenizer==null || !tokenizer.hasMoreTokens()) {
  72. try {
  73. tokenizer = new StringTokenizer(br.readLine());
  74. }catch(IOException e) {
  75. throw new RuntimeException(e);
  76. }
  77. }
  78. return tokenizer.nextToken();
  79. }
  80. public int nextInt() {
  81. return Integer.parseInt(next());
  82. }
  83. }
  84. }

【Codeforces 479D】Long Jumps的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  3. 【32.22%】【codeforces 602B】Approximating a Constant Range

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  5. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  6. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  7. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  8. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  9. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

随机推荐

  1. oracle从子表取出前几行数据:

    取排序后的前几行,应该用: select * from(select * from test order by stamp desc) where rownum<= 6  (表示排序后取前几行) ...

  2. 【410】Linux 系统 makefile 文件

    makefile 主要是用来合并编译文件 CC = gcc puzzle: puzzle.c boardADT.o $(CC) puzzle.c boardADT.o -o puzzle -lm bo ...

  3. Eclipse导入Java 的jar包的方法

    打开eclipse1.右击要导入jar包的项目,点properties 2.左边选择java build path,右边选择libraries 3.选择add External jars 4.选择ja ...

  4. sql让时间调前,调后的语句

    时间调前,调后 select billid,DATEADD(mm,2,billdate) from bi_Bill 注:用dateadd(/时间年/月/日,调前或后多少,字段) mm为月份,2为调前两 ...

  5. P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  6. 题解报告:hdu 1686 Oulipo(裸KMP)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  7. 292 Nim Game Nim游戏

    您和您的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 到 3 块石头. 拿掉最后一块石头的人就是胜利者.由您来开局.你们两个都是聪明人,相信都有最佳的游戏策略. 请编写一个函 ...

  8. Kali linux 2016.2(Rolling)安装之后的常用配置

    前言 使用默认的Kali Linux设置来学习是可以的,但是我们通常要修改系统的一些基本设置,来最大化使用Kali平台的功能. 以下内容 网络的基础知识 使用图形用户界面来配置网卡 使用命令行来配置网 ...

  9. iOS 项目中的常见文件

    iOS的笔记-项目中的常见文件   新建一个项目之后,有那么多的文件,下面介绍一下主要的几个. 1.文件名 (1)AppDelegate UIApplication的代理,app收到干扰的时候,进行处 ...

  10. IFormattable,ICustomFormatter, IFormatProvider接口

    定                 义 1.IFormattable   提供一种功能,用以将对象的值格式化为字符串表示形式. 2.IFormatProvider  提供用于检索控制格式化的对象的机制 ...