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

【题意】

让你在n个数字中再加入一个数字
使得这n+1个数字排序之后
相邻两个数字的差都相同

【题解】

注意相邻为0的情况 这种情况 只有全都相同才行 只有一种情况
然后就是样例里的a[i]-a[i-1]只有两种数字
然后较小的a[i]-a[i-1]有n-2个,较大的a[i]-a[i-1]有1个,然后较大的是较小的两倍
注意这些细节就好了

【代码】

  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 = (int)1e5;
  16. static class Task{
  17. int n;
  18. int a[] = new int[N+10];
  19. HashMap<Integer,Integer> dic = new HashMap<>();
  20. public void solve(InputReader in,PrintWriter out) {
  21. n = in.nextInt();
  22. for (int i = 1;i <= n;i++) a[i] = in.nextInt();
  23. Arrays.sort(a, 1,n+1);
  24. if (n==1) {
  25. out.println(-1);
  26. }else if (n==2) {
  27. if (a[1]==a[2]) {
  28. out.println(1);
  29. out.println(a[1]);
  30. }else {
  31. if ( (a[1]+a[2])%2==0) {
  32. int temp = a[2]-a[1];
  33. out.println(3);
  34. out.print((a[1]-temp)+" "+((a[1]+a[2])/2)+" "+(a[2]+temp) );
  35. }else {
  36. int temp = a[2]-a[1];
  37. out.println(2);
  38. out.println((a[1]-temp)+" "+(a[2]+temp));
  39. }
  40. }
  41. }else {
  42. int temp = a[2]-a[1];
  43. boolean ok = true;
  44. for (int i = 3;i <= n;i++) {
  45. if (a[i]-a[i-1]!=temp) {
  46. ok = false;
  47. }
  48. }
  49. if (ok) {
  50. if (temp==0) {
  51. out.println(1);
  52. out.println(a[1]);
  53. }else {
  54. out.println(2);
  55. out.println((a[1]-temp)+" "+(a[n]+temp));
  56. }
  57. }else {
  58. for (int i = 2;i <= n;i++) {
  59. if (dic.containsKey(a[i]-a[i-1])) {
  60. int x = dic.get(a[i]-a[i-1]);
  61. dic.put(a[i]-a[i-1],x+1);
  62. }else {
  63. dic.put(a[i]-a[i-1], 1);
  64. }
  65. }
  66. if (dic.size()>2) {
  67. out.println(0);
  68. }else {
  69. Iterator it = dic.keySet().iterator();
  70. int temp1 = (int)it.next();
  71. int temp2 = (int)it.next();
  72. if (temp1>temp2) {
  73. int xx = temp1;temp1 = temp2;temp2 = xx;
  74. }
  75. if ( (temp2 == temp1*2) && ( (int)dic.get(temp2) )==1 ) {
  76. out.println(1);
  77. for (int i = 2;i <= n;i++) {
  78. if (a[i]-a[i-1]==temp2) {
  79. out.println(a[i-1]+temp1);
  80. }
  81. }
  82. }else {
  83. out.println(0);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. static class InputReader{
  91. public BufferedReader br;
  92. public StringTokenizer tokenizer;
  93. public InputReader(InputStream ins) {
  94. br = new BufferedReader(new InputStreamReader(ins));
  95. tokenizer = null;
  96. }
  97. public String next(){
  98. while (tokenizer==null || !tokenizer.hasMoreTokens()) {
  99. try {
  100. tokenizer = new StringTokenizer(br.readLine());
  101. }catch(IOException e) {
  102. throw new RuntimeException(e);
  103. }
  104. }
  105. return tokenizer.nextToken();
  106. }
  107. public int nextInt() {
  108. return Integer.parseInt(next());
  109. }
  110. }
  111. }

【Codeforces 382C】Arithmetic Progression的更多相关文章

  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 604D】Moodular Arithmetic

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  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. J20170527-ts

    足場 立脚点.脚手架 scaffold ハイパーリンク 超链接 hyperlink アンカータグ        锚标签 でしゃばり 多嘴.多事.多管闲事的人,好出风头的人 でしゃばる 多管闲事 節介 ...

  2. 清北考前刷题day6下午好

    /* 贪心 负数一定不取 枚举最高位是1 且答案取为0的 位置, 更新答案. */ #include<iostream> #include<cstdio> #include&l ...

  3. $CF1153A\ Serval\ and\ Bus$

    看大佬的代码都好复杂(不愧是大佬\(orz\) 蒟蒻提供一种思路 因为求的是最近的车对吧\(qwq\) 所以我们可以用一个\(while\)循环所以没必要去用什么 \(for...\) 至于这是\(d ...

  4. 树莓派 关闭屏保 / RaspberryPi turn off ScreenSaver / RaspberryPi disable screen off

    安装xscreensaver并配置 见:https://www.raspberrypi.org/forums/viewtopic.php?t=57552

  5. WCF学习笔记(2)-WCF的通讯过程

    一.WCF中的ABC 场景:公司让你送一份合同文件,送文件的过程你可以选择的交通方式有打的,地铁或公交. 到了对方公司后,你要找到某负责人,并且要一份收到合同文件的回执和相应文件 要完成这项工作任务主 ...

  6. eclipse安装提示错误:Failed to load JNI shared library "D:\jdk1.7\client\jvm.dll"

    错误截图如下 原因是jdk32位,eclipse64位导致,修改jdk版本为64位或者修改ecipse版本为32位即可.

  7. Python 设计模式--策略模式

    策略模式(Strategy Pattern) 策略模式是一种与行为相关的设计模式,允许你在运行时根据指定的上下文确定程序的动作.可以在两个类中封装不同的算法,并且在程序运行时确定到底执行哪中策略. 特 ...

  8. [ NOI 2001 ] 方程的解数

    \(\\\) \(Description\) 已知一个 \(N\) 元高次方程: \[ k_1x_1^{p_1}+k_2x_2^{p_2}+...+k_nx_n^{p_n}=0 \] 要求所有的 \( ...

  9. [Python] xrange和range的使用区别

    zhuan:https://blog.csdn.net/humanking7/article/details/45950967 range 函数说明:range([start,] stop[, ste ...

  10. MySQL详解(18)-----------分页方法总结

    ---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适应场景: 适用于数据量较少的情况(元组百/千 ...