【Codeforces 382C】Arithmetic Progression

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

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

【题解】

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

【代码】

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 = (int)1e5;
    static class Task{
        
        int n;
        int a[] = new int[N+10];
        HashMap<Integer,Integer> dic = new HashMap<>();
        
        public void solve(InputReader in,PrintWriter out) {
        	n = in.nextInt();
        	for (int i = 1;i <= n;i++) a[i] = in.nextInt();
        	Arrays.sort(a, 1,n+1);
        	if (n==1) {
        		out.println(-1);
        	}else if (n==2) {
        		if (a[1]==a[2]) {
        			out.println(1);
        			out.println(a[1]);
        		}else {
        			if ( (a[1]+a[2])%2==0) {
        				int temp = a[2]-a[1];
        				out.println(3);
        				out.print((a[1]-temp)+" "+((a[1]+a[2])/2)+" "+(a[2]+temp) );
        			}else {
        				int temp = a[2]-a[1];
        				out.println(2);
        				out.println((a[1]-temp)+" "+(a[2]+temp));
        			}
        		}
        	}else {
        		int temp = a[2]-a[1];
        		boolean ok = true;
        		for (int i = 3;i <= n;i++) {
        			if (a[i]-a[i-1]!=temp) {
        				ok = false;
        			}
        		}
        		if (ok) {
        			if (temp==0) {
        				out.println(1);
        				out.println(a[1]);
        			}else {
        				out.println(2);
        				out.println((a[1]-temp)+" "+(a[n]+temp));
        			}
        		}else {
        			 for (int i = 2;i <= n;i++) {
        				 if (dic.containsKey(a[i]-a[i-1])) {
        					 int x = dic.get(a[i]-a[i-1]);
        					 dic.put(a[i]-a[i-1],x+1);
        				 }else {
        					 dic.put(a[i]-a[i-1], 1);
        				 }
        			 }
        			 if (dic.size()>2) {
        				 out.println(0);
        			 }else {
        				 Iterator it = dic.keySet().iterator();
        				 int temp1 = (int)it.next();
        				 int temp2 = (int)it.next();
        				 if (temp1>temp2) {
        					 int xx = temp1;temp1 = temp2;temp2 = xx;
        				 }
        				 if ( (temp2 == temp1*2) && ( (int)dic.get(temp2) )==1 ) {
        					 out.println(1);
        					 for (int i = 2;i <= n;i++) {
        						 if (a[i]-a[i-1]==temp2) {
        							 out.println(a[i-1]+temp1);
        						 }
        					 }
        				 }else {
        					 out.println(0);
        				 }
        			 }
        		}
        	}
        }
    }

    

    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());
        }
    }
}
posted @ 2019-03-14 18:28  AWCXV  阅读(138)  评论(0编辑  收藏  举报