【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());
}
}
}
【Codeforces 382C】Arithmetic Progression的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 604D】Moodular Arithmetic
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【51.27%】【codeforces 604A】Uncowed Forces
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- FastDFS的介绍
FastDFS的介绍 FastDFS FastDFS是由国人余庆所开发,其项目地址: https://github.com/happyfish100 FastDFS是一个轻量级的开源分布式文件系统,主 ...
- LaTex简历排版
为了写好简历,倒腾了好几天的Latex,主要是因为中文支持问题还有morderncv的更新问题... 1. http://www.ctex.org/CTeXDownload/ 建议下载v2.9.2. ...
- bzoj 1652: [Usaco2006 Feb]Treats for the Cows【区间dp】
裸的区间dp,设f[i][j]为区间(i,j)的答案,转移是f[i][j]=max(f[i+1][j]+a[i](n-j+i),f[i][j-1]+a[j]*(n-j+i)); #include< ...
- nat模式下更改网络环境, 虚拟机中Linux无法上网的问题
出现的问题: 1.ifconfig -a 命令下会出现eth0信息中无ip地址等等信息: 2.无法ping通baidu,也就是无法上网: 3.ping 8.8.8.8 提示 connect:netwo ...
- Get 和 Post 使用篇(1)
1.Post 请求发送方式 实例: const string sResponseEncoding = "gb2312"; //测试文本信息 string postText = &q ...
- c语言小项目-使用mysql数据库的图书管理系统
VS2013通过MySQL方式连接到MySQL MySQL官网上C++的API有两个.一个是很成熟的mysql++,另一个是MySQL Connector/C++,近两年才出的,模仿JDBC做的,封装 ...
- 暴力/思维 HDOJ 5386 Cover
题目传送门 /* 题意:给出刷墙的所有的方法,求一种顺序,使得原矩阵刷成目标矩阵 暴力:(题解)我们只要每次找一行或一列颜色除了0都相同的,然后如果有对应的操作,就把这行这列都赋值成0即可 */ /* ...
- C# 事件与委托(转载)
委托的定义 delegate 是 C# 中的一种类型,它实际上是一个能够持有对某个方法的引用的类.与其它的类不同,delegate 类能够拥有一个方法的签名(signature),并且它"只 ...
- 264 Ugly Number II 丑数 II
编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...
- [转]Windows Azure平台简介(一):定位与产品结构
本文转自:http://blog.csdn.net/azurechina/article/details/5592236 http://blogs.msdn.com/b/azchina/archive ...