【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) = ...
随机推荐
- J20170527-ts
足場 立脚点.脚手架 scaffold ハイパーリンク 超链接 hyperlink アンカータグ 锚标签 でしゃばり 多嘴.多事.多管闲事的人,好出风头的人 でしゃばる 多管闲事 節介 ...
- 清北考前刷题day6下午好
/* 贪心 负数一定不取 枚举最高位是1 且答案取为0的 位置, 更新答案. */ #include<iostream> #include<cstdio> #include&l ...
- $CF1153A\ Serval\ and\ Bus$
看大佬的代码都好复杂(不愧是大佬\(orz\) 蒟蒻提供一种思路 因为求的是最近的车对吧\(qwq\) 所以我们可以用一个\(while\)循环所以没必要去用什么 \(for...\) 至于这是\(d ...
- 树莓派 关闭屏保 / RaspberryPi turn off ScreenSaver / RaspberryPi disable screen off
安装xscreensaver并配置 见:https://www.raspberrypi.org/forums/viewtopic.php?t=57552
- WCF学习笔记(2)-WCF的通讯过程
一.WCF中的ABC 场景:公司让你送一份合同文件,送文件的过程你可以选择的交通方式有打的,地铁或公交. 到了对方公司后,你要找到某负责人,并且要一份收到合同文件的回执和相应文件 要完成这项工作任务主 ...
- eclipse安装提示错误:Failed to load JNI shared library "D:\jdk1.7\client\jvm.dll"
错误截图如下 原因是jdk32位,eclipse64位导致,修改jdk版本为64位或者修改ecipse版本为32位即可.
- Python 设计模式--策略模式
策略模式(Strategy Pattern) 策略模式是一种与行为相关的设计模式,允许你在运行时根据指定的上下文确定程序的动作.可以在两个类中封装不同的算法,并且在程序运行时确定到底执行哪中策略. 特 ...
- [ NOI 2001 ] 方程的解数
\(\\\) \(Description\) 已知一个 \(N\) 元高次方程: \[ k_1x_1^{p_1}+k_2x_2^{p_2}+...+k_nx_n^{p_n}=0 \] 要求所有的 \( ...
- [Python] xrange和range的使用区别
zhuan:https://blog.csdn.net/humanking7/article/details/45950967 range 函数说明:range([start,] stop[, ste ...
- MySQL详解(18)-----------分页方法总结
---方法1: 直接使用数据库提供的SQL语句---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N---适应场景: 适用于数据量较少的情况(元组百/千 ...