Arithmetic Progression

来源:互联网 发布:手机游戏下载java 编辑:程序博客网 时间:2024/05/01 16:34
 
 
→ Virtual participation
Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.



  • Problems
  • Submit
  • Status
  • Standings
  • Custom test

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 anarithmetic progression is such sequence of numbersa1, 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 resultingn + 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 exceed108 or even be negative (see test samples).

Examples

Input

34 1 7

Output

2-2 10

Input

110

Output

-1

Input

41 3 5 9

Output

17

Input

44 3 4 5

Output

0

Input

22 4

Output

30 3 6



题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能的情况,并输出这些数。

     n = 1 、 n = 2 和 整个序列是常数列 的情况比较容易判断。不过要注意n = 2的时候,也需要判断两个数之间是否也可以通过插入一个数来构成等差数列。

     关键是讨论n>=3的情况。预处理:把整个输入序列从小到大排序。之后,得到公差是第一要务!如果可以从中插入一个数(这时一定不是两端,也就是说这两种情况是互斥的),那么两个相邻的数之差 = 公差的次数会是最多的!只要找到这个差出现不少于2次以上,这个差就是公差。

     确定公差之后,后面的情况就比较容易判断。插入该数的两个相邻数之间的差不可能等于公差,而该数是否合法,可以通过这两个相邻数中较小的一个 + 2倍公差,能否得到较大的数来判断。

    如果插入的数不在中间,那么有可能是无解(输出0,插入哪个位置都不能使得序列成为等差数列)或者是从两端插入(输入的序列已经是等差数列)

     还有一种情况要特别注意,当序列只有3个数的时候,并且可以从中插入一个数成为等差数列的情况。此时公差取较小的那个差,因为较大的差之间比较小的差能插入该数的可能性更大。例如序列:1 3 4,我们会取1作为公差,而不是2,此时可在1和3之间插入2来构成等差数列




#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>using namespace stdconst int maxn = 1e5 + 5;int a[maxn];  int main() {     int i, cnt, n, d, ans, td, td1, c1, c2;    while (scanf("%d", &n) != EOF)    {        for (i = 1; i <= n; i++)             scanf("%d", &a[i]);         sort(a+1, a+n+1);         d = a[2]-a[1];         if (n == 1)     // 可以插入无限个数,只要等于a[1]就行             printf("-1\n");         else if (a[1] == a[n] && n >= 2)  // 常数列             printf("1\n%d\n", a[1]);         else if (n == 2)         {             if ((a[1] + a[2]) % 2)  // 判断相邻数之间是否也可以插入                 printf("2\n%d %d\n", a[1]-d, a[2]+d);             else                 printf("3\n%d %d %d\n", a[1]-d, (a[1]+a[2])/2,  a[2]+d);         }         else         {            if (n >= 3)            {                 c2 = c1 = 1;                 td1 = a[3]-a[2];                 if (td1 != d)                 {                     for (i = 4; i <= n; i++)                     {                         int td = a[i] - a[i-1];    // 出现次数>=2的差即为整个序列的公差                    if (td == td1)                            c2++;                         else                             c1++;                         if (c1 >= 2 || c2 >= 2)                             break;                    }                }                 if (c2 >= c1 && n >= 3 && d > td1)    // 最终确定公差                     d = td1;                         }             cnt = 0;            for (i = 2; i <= n; i++)            {                td = a[i] - a[i-1];                if (d != td)                {                    cnt++;                     if ((a[i-1]+a[i]) % 2 || a[i-1]+2*d != a[i])                     {                         cnt++;                         break;                     }                     else                         ans = a[i-1] + d;                 }             }            if (!cnt)                 printf("2\n%d %d\n", a[1]-d, a[n]+d);            else if (cnt == 1)                 printf("1\n%d\n", ans);             else                 printf("0\n");        }    }    return 0; }


0 0
原创粉丝点击