美文网首页
CUC-SUMMER-CONTEST-2-A

CUC-SUMMER-CONTEST-2-A

作者: Nioge | 来源:发表于2017-08-18 00:45 被阅读0次
    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;
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:CUC-SUMMER-CONTEST-2-A

          本文链接:https://www.haomeiwen.com/subject/ntdorxtx.html