您的位置:首页 > 其它

Codeforces 382 C. Arithmetic Progression

2014-01-18 02:00 543 查看
先由小到大排序

1:只有一个数  无限多

2:存在3种以上的间距 无

3:出现2种间距

大的间距只有1个,且长度是小的两倍  1种

大的间距有几个  无

4:只有1种间距

只出现一次  一头一尾  或者 加上中间

**仅有的间距是0  1种

C. Arithmetic Progression

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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 (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.

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).

Sample test(s)

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


#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>

using namespace std;

int n,ct=0;
int a[200200];

set<int> vis;

struct node
{
int dist,num,pos;
}D[5];

int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
}
if(n==1)
{
printf("-1\n"); return 0;
}

sort(a,a+n);

bool flag=true;
for(int i=1;i<n;i++)
{
int dd=a[i]-a[i-1];
if(vis.count(dd)==0)
{
vis.insert(dd);
D[ct].dist=dd;
D[ct].num=1;
D[ct].pos=i;
ct++;
}
else
{
for(int j=0;j<ct;j++)
{
if(D[j].dist==dd)
{
D[j].num++;
break;
}
}
}

if(ct>=3)
{
flag=false; break;
}
}

if(flag==false)
{
printf("0\n"); return 0;
}

if(ct==1&&D[0].num!=1)
{
int Dd=D[0].dist;
if(Dd!=0)
{
printf("2\n");
printf("%d %d\n",a[0]-Dd,a[n-1]+Dd);
}
else
{
printf("1\n");
printf("%d\n",a[0]);
}
return 0;
}

if(ct==1&&D[0].num==1)
{
int Dd=D[0].dist;
if(Dd)
{
if(Dd%2==0)
{
printf("3\n");
printf("%d %d %d\n",a[0]-Dd,a[0]+Dd/2,a[n-1]+Dd);
}
else
{
printf("2\n");
int Dd=D[0].dist;
printf("%d %d\n",a[0]-Dd,a[n-1]+Dd);
}
return 0;
}
else
{
printf("1\n");
printf("%d\n",a[0]);
return 0;
}
}

int dade=(D[0].dist>D[1].dist)? 0 : 1;

if(D[dade].num!=1)
{
printf("0\n"); return 0;
}
else
{
if(D[dade].dist!=D[1-dade].dist*2)
{
printf("0\n"); return 0;
}
}

printf("1\n");
printf("%d\n",a[D[dade].pos-1]+(a[D[dade].pos]-a[D[dade].pos-1])/2);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces