【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) = ...
随机推荐
- A brief preview of the new features introduced by OpenGL 3.3 and 4.0
A brief preview of the new features introduced by OpenGL 3.3 and 4.0 The Khronos Group continues t ...
- wamp集成环境下帝国备份出错
我在本地wamp环境下面使用帝国备份王时,报错信息如下: Parse error: syntaxerror, unexpected $end in D:wampwwwhuifuclassfunctio ...
- [Qt Creator 快速入门] 第3章 窗口部件
从这一章开始正式接触Qt的窗口部件.在第2章曾看到 Qt Creator 提供的默认基类只有 QMainWindow.QWidget 和 QDialog 这3种.QMainWindow 是带有菜单栏和 ...
- 模拟 HDOJ 4552 Running Rabbits
题目传送门 /* 模拟:看懂题意,主要是碰壁后的转向,笔误2次 */ #include <cstdio> #include <algorithm> #include <c ...
- SQL server存储过程及触发器基础
存储过程:就像函数一样的会保存在数据库中-->可编程性 --> 存储过程-----------------------------------------------------创建存储过 ...
- Java 8 (8) 默认方法
传统上,Java程序的接口是将相关方法按照预定组合到一起的方式.实现接口的类必须为接口中定义的方法提供一个实现,或者从父类中集成它的实现.但是,一旦类库的设计者需要更新接口,向接口中加入新的方法时候, ...
- Python 设计模式--策略模式
策略模式(Strategy Pattern) 策略模式是一种与行为相关的设计模式,允许你在运行时根据指定的上下文确定程序的动作.可以在两个类中封装不同的算法,并且在程序运行时确定到底执行哪中策略. 特 ...
- can't set blob value on that column
MySQL_Prepared_Statement::setBlob: can't set blob value on that column, MySQL error code:0, SQLState ...
- ElasticSearch 安装使用
安装: 1.下载ElasticSearch.解压到相关文件夹 2.运行elasticsearch.bat,启动程序 3.在浏览器输入:http://localhost:9200/,显示相关Es内容即安 ...
- react Native环境 搭建
react Native的优点:跨平台 低投入高回报 性能高 支持动态更新.一才两用(ios和Android) 开发成本第 代码复用率高.windows环境搭建react Native开发环境1.安装 ...