@Chilling
2017-02-16T10:56:07.000000Z
字数 2630
阅读 862
杂
Description
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:
a2 - a1 = a3 - a2 = a4 - a3 = ... = ai + 1 - ai = ... = an - an - 1.
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.
Input
The first line contains integer n — 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 .
Output
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).
Example
Input
3
4 1 7
Output
2
-2 10
Input
1
10
Output
-1
Input
4
1 3 5 9
Output
1
7
Input
4
4 3 4 5
Output
0
Input
2
2 4
Output
3
0 3 6
题意:输入n个数,判断再加入一个数能不能使这n+1个数成为等差数列,如果可以,问加入的数字有多少种可能,输出有几种可能,并且按从小到大的顺序输出他们。如果有无数种可能,输出-1。
分析:首先按照从小到大的顺序对这些数字排序。
后面就是……惊天大模拟
注意:若相邻两个数的差有2个不同的,其中一个差必定只出现一次,并且是更大的。如:1,2,4,5,差1出现2次,差2出现一次,添加3就可以构成等差数列,而2,4,5,7中,1出现1次,2出现两次,不能添加一个数字构成等差数列。
#include<stdio.h>
#include<algorithm>
#include<map>
#include<string.h>
#define LL long long
using namespace std;
LL a[100005];
LL b[100005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
map<LL,LL>m;
map<LL,LL>::iterator it;
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
if(n==1)
{
printf("-1\n");
continue;
}
sort(a,a+n);
for(int i=n-1;i-1>=0;i--)
{
int j=i;
m[a[i]-a[i-1]]++;
b[j]=a[i]-a[i-1];
}
if(m.size()>2)
printf("0\n");
else
{
if(m.size()==1)
{
LL x,y;
for(it=m.begin();it!=m.end();it++)
x=it->first,y=it->second;
if(x==0)
printf("1\n%lld\n",a[0]);
else if((x%2==1)||y>1)
printf("2\n%lld %lld\n",a[0]-x,a[n-1]+x);
else
printf("3\n%lld %lld %lld\n",a[0]-x,(a[0]+a[n-1])/2,a[n-1]+x);
}
else
{
LL x[3],y[3],j=0;
LL xx=-1;
for(it=m.begin();it!=m.end();it++)
{
x[j]=it->first;
y[j++]=it->second;
}
if(y[0]!=1&&y[1]!=1)
printf("0\n");
else
{
if((x[0]>x[1]&&y[0]>y[1])||(x[0]<x[1]&&y[0]<y[1]))
printf("0\n");
else
{
if(y[0]<y[1])
{
if(x[0]==2*x[1])
xx=x[1];
else
printf("0\n");
}
if(y[0]>y[1])
{
if(2*x[0]==x[1])
xx=x[0];
else
printf("0\n");
}
else
{
if(x[0]==2*x[1])
xx=x[1];
if(x[1]==2*x[0])
xx=x[0];
else
printf("0\n");
}
if(xx!=-1)
{
int aa;
for(aa=0;aa<n;aa++)
{
if(b[aa]==2*xx)
break;
}
printf("1\n%d\n",a[aa]-xx);
}
}
}
}
}
}
return 0;
}