CUC-SUMMER-CONTEST-2-A

A - Arithmetic Progression
CodeForces - 382C

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

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


题意:给你一个序列,问加入一个数可不可以使数列变成等差数列,如果可以输出可以加的数

解法:分情况讨论,序列本身就是等差数列时,不是等差数列时,单独讨论长度为1,2时,很多坑,很难ac,高级水题

代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100010;
int a[maxn];
int main()
{
    int n,aa=0;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(i!=0){
            if(a[i]!=a[i-1])
                aa=1;
        }
    }
    sort(a,a+n);
    if(n==1){
        cout<<-1<<endl;
        return 0;
    }
    if(aa==0){
        cout<<1<<endl;
        cout<<a[0]<<endl;
        return 0;
    }
    if(n==2){
        if((a[1]-a[0])%2==1){
            cout<<2<<endl;
            cout<<a[0]-(a[1]-a[0])<<" "<<a[1]+(a[1]-a[0])<<endl;
            return 0;
        }
        else{
            cout<<3<<endl;
            cout<<a[0]-(a[1]-a[0])<<" "<<a[0]+(a[1]-a[0])/2<<" "<<a[1]+(a[1]-a[0])<<endl;
            return 0;
        }

    }
    int sub=a[1]-a[0],cnt=0,x;
    for(int i=2;i<n;i++){
        int sub1=a[i]-a[i-1];
        if(sub1!=sub){
            cnt++;
            x=i;
        }
    }
    if(n==3&&cnt==1&&a[2]-a[1]<sub){
        cnt=2;
    }
    if(cnt==0){
        cout<<2<<endl;
        cout<<a[0]-sub<<" "<<a[n-1]+sub<<endl;
        return 0;
    }
    if(cnt==1){
        if((a[x]-a[x-1])/2.0==double(sub)){
            cout<<1<<endl;
            cout<<(a[x]+a[x-1])/2<<endl;
            return 0;
        }
        else{
            cout<<0<<endl;
            return 0;
        }
    }
    if(cnt>1){
        int tt=a[2]-a[1],flag=0;
        for(int i=3;i<n;i++){
            if(a[i]-a[i-1]!=tt){
                flag=1;
                break;
            }
        }
        if(flag==0){
            if((a[1]-a[0])/2.0==double(tt)){
                cout<<1<<endl;
                cout<<(a[1]+a[0])/2<<endl;
                return 0;
            }
            else{
                cout<<0<<endl;
                return 0;
            }
        }
        else{
            cout<<0<<endl;
            return 0;
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,993评论 0 23
  • 我认为这只是一个普通的星期六,即使我并不在上海,但去上海学习和交流,是我创业以来的习惯,不只我去上海已经麻木了,朋...
    晶爷阅读 373评论 0 3
  • 这是牛皮纸粉彩铅画试水第二张。个人感觉比第一张好一些。这次用到了棕色、熟褐、米色、白色、黑色,以及粗炭条、棉签、抽...
    陈狂阅读 2,373评论 92 109
  • 青春是美丽的,它的一生虽然短暂,但却绚烂多彩。青春是每个人无比向往、眷恋的年华。当然,身处青春时光的我们也是最好...
    谁_30ff阅读 128评论 0 0