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

【题意】

让你在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());
}
}
}

【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. bzoj 3401: [Usaco2009 Mar]Look Up 仰望【单调栈】

    用单调递减的栈从后往前扫一遍即可 #include<iostream> #include<cstdio> using namespace std; const int N=10 ...

  2. bzoj 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏【模拟】

    模拟 #include<iostream> #include<cstdio> using namespace std; int n,ans; int main() { scan ...

  3. cookie使用详解

    cookie是用来保存客户资料的好方法,与同样可以用来保存客户资料的 session不同的是,session是把资料保存在服务器端,而cookie是把资料保存在客户端,我们平常接触的最多的cookie ...

  4. macbookpro安装Ubuntu16.04.1 LTS爬坑之旅。亲测有效(集众家之长)。安装时间为2017-11-19。

    1.格式化U盘 要求:(1)切换分区格式为Mac OS扩展 (日志型):(2)方案(scheme)设置为:GUID Partition Map:如图(使用mac自带磁盘工具) 2.给Ubuntu划分磁 ...

  5. [转]c++中的string常用函数用法总结

    标准c++中string类函数介绍 注意不是CString之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为 ...

  6. yii框架下jquery在ajax更新后失效问题

    解决方案,以live的形式重新绑定一次, /***回复隐藏收起效果***/ $(".btn-reply").live('click',function(event){ var da ...

  7. C# 从服务器下载文件并保存到客户端

    参考代码: using System; using System.Net; namespace HT.SIHONG.Common.Utility { public class DownloadFile ...

  8. 240 Search a 2D Matrix II 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7 ...

  9. Smarty的应用

    smarty模板的核心是一个类,下载好的模板中有这么几个重要的文件夹 (1)libs核心文件夹(2)int.inc.php这是入口文件(3)plugins:自己写的插件文件夹(4)templates_ ...

  10. mongo 3.4分片集群系列之七:配置数据库管理

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...