Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1, a2, ..., an of length n, that the following condition fulfills:
For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not.
Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards).
Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.
The first line contains integer n (1 ≤ n ≤ 105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108.
If Arthur can write infinitely many distinct integers on the card, print on a single line -1.
Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).
3 4 1 7
2 -2 10
1 10
-1
4 1 3 5 9
1 7
4 4 3 4 5
0
2 2 4
3 0 3 6
这个题差点没作死我,问你等差数列再加一个数的所有情况,我列举了每一种情况。。
用代码讲吧;
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[200000],n;
int b[200000];
bool judge(int a[]){//判断已经是等差数列
int k=a[1]-a[0];
for(int i=1;i<n;i++){
if(a[i]-a[i-1]!=k)
return false;
}
return true;
}
bool solve(int a[]){
int ans=1,top=0;
for(int i=1;i<n;i++){
b[top++]=a[i]-a[i-1];
}
sort(b,b+top);
int ans2=1,kk=b[0];
for(int i=1;i<top;i++){
if(b[i]!=b[i-1]){//看有几个不同的公差
ans++;
}
if(b[i]>kk){//判断最大的间距只能有一个
kk=b[i];
ans2=1;
}
else if(b[i]==kk){
ans2++;
}
}
if(ans>2||ans2!=1)//最大的间距只能有一个且不同的公差只能有两个
return false;
int k1=b[0],k2;
for(int i=0;i<top;i++){
if(b[i]!=k1)
k2=b[i];
}
if(!k1||!k2)//公差不能为0
return false;
if((k1/2==k2&&k1%2==0)||(k2/2==k1&&k2%2==0))//大的公差恰好是小的两倍
return true;
return false;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
if(n==1){//只有一个数的情况
return 0*printf("-1\n");
}
if(n==2){//数列中有两个数分3种情况
if(a[0]==a[1]){
printf("1\n%d\n",a[0]);
}
else if(a[1]-a[0]==1||(a[1]-a[0])%2==1){
printf("2\n%d %d\n",a[0]-(a[1]-a[0]),a[1]+(a[1]-a[0]));
}
else{
printf("3\n%d %d %d\n",a[0]-(a[1]-a[0]),(a[1]+a[0])/2,a[1]+(a[1]-a[0]));
}
return 0;
}
if(judge(a)){//已经是等差数列
if(a[0]!=a[1]){
printf("2\n");
printf("%d %d\n",a[0]-(a[1]-a[0]),a[n-1]+(a[n-1]-a[n-2]));
}
else{
printf("1\n");
printf("%d\n",a[0]);
}
}
else{
if(!solve(a)){
return 0*printf("0\n");
}
int k1=a[1]-a[0],k2=a[0];
for(int i=1;i<n;i++){
if(a[i]-a[i-1]>k1){
k1=a[i]-a[i-1];
k2=a[i-1];
}
}//输出
printf("1\n");
printf("%d\n",k2+k1/2);
}
return 0;
}