天天看点

Codeforces Round #224 (Div. 2) C. Arithmetic Progression【构造等差数列】

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

Examples 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

input

2
2 4
      

output

3
0 3 6      

原题链接:http://codeforces.com/problemset/problem/382/C

题意:给你一个序列,问你是否可以添加一个数字是的整个序列变为等差数列。可以的话输出方案数和添加的数字。

分析:

1、如果只有一个数字,输出-1;

2、如果有两个数字:

2.1、这两个数字相同的话只有一种情况,输出1和a[0];

2.2、这两个数的差为偶数:则有三种情况//10 20

2.3、这两个数的差为奇数,则有两种情况//2 5

3、有多个数字。先找出最小公差。具体看代码:

参考博客:http://blog.csdn.net/u011470356/article/details/23294961

http://blog.csdn.net/ida0918/article/details/52431803

AC代码:

/**
* 行有余力,则来刷题!
  * 博客链接:http://blog.csdn.net/hurmishine
  *
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0; i<n; i++)
            cin>>a[i];
        sort(a,a+n);
        if(n==1)
            cout<<"-1"<<endl;
        else if(n==2)
        {
            int d=a[1]-a[0];
            if(d==0)
                cout<<"1\n"<<a[0]<<endl;
            else if(d%2==0)
            {
                cout<<"3"<<endl;
                cout<<a[0]-d<<" "<<a[0]+d/2<<" "<<a[1]+d<<endl;
            }
            else//2 5
            {
                cout<<"2\n"<<a[0]-d<<" "<<a[1]+d<<endl;
            }
        }
        else
        {
            int flag=0;
            int d=1e8;
            for(int i=1; i<n; i++)
            {
                int dd=a[i]-a[i-1];
                if(dd<d)
                    d=dd;
            }
            int cnt=0;
            for(int i=1; i<n; i++)
            {
                if(a[i]-a[i-1]!=d)
                {
                    flag=i;
                    cnt++;
                }
            }
            if(cnt==0&&d==0)//1 1 1 1
                cout<<"1\n"<<a[0]<<endl;
            else if(cnt==0&&d!=0)//1 2 3 4
                cout<<"2\n"<<a[0]-d<<" "<<a[n-1]+d<<endl;
            else if(cnt==1&&a[flag]-a[flag-1]==2*d)//2 4 8 10
                cout<<"1\n"<<a[flag-1]+d<<endl;
            else
                cout<<"0\n";
        }
    }
    return 0;
}
           
上一篇: 小学奥数题