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
0
input
2
2 4
output
3
0 3 6
题目链接:http://codeforces.com/contest/382/problem/C


这个题差点没作死我,问你等差数列再加一个数的所有情况,我列举了每一种情况。。

用代码讲吧;

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[200000],n;
int b[200000];
bool judge(int a[]){//判断已经是等差数列
    int k=a[1]-a[0];
    for(int i=1;i<n;i++){
        if(a[i]-a[i-1]!=k)
            return false;
    }
    return true;
}
bool solve(int a[]){
    int ans=1,top=0;
    for(int i=1;i<n;i++){
        b[top++]=a[i]-a[i-1];
    }
    sort(b,b+top);
    int ans2=1,kk=b[0];
    for(int i=1;i<top;i++){
        if(b[i]!=b[i-1]){//看有几个不同的公差
            ans++;
        }
        if(b[i]>kk){//判断最大的间距只能有一个
            kk=b[i];
            ans2=1;
        }
        else if(b[i]==kk){
            ans2++;
        }
    }
    if(ans>2||ans2!=1)//最大的间距只能有一个且不同的公差只能有两个
        return false;
    int k1=b[0],k2;
    for(int i=0;i<top;i++){
        if(b[i]!=k1)
            k2=b[i];
    }
    if(!k1||!k2)//公差不能为0
        return false;
    if((k1/2==k2&&k1%2==0)||(k2/2==k1&&k2%2==0))//大的公差恰好是小的两倍
        return true;
    return false;
}
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    if(n==1){//只有一个数的情况
        return 0*printf("-1\n");
    }
    if(n==2){//数列中有两个数分3种情况
        if(a[0]==a[1]){
            printf("1\n%d\n",a[0]);
        }
        else if(a[1]-a[0]==1||(a[1]-a[0])%2==1){
            printf("2\n%d %d\n",a[0]-(a[1]-a[0]),a[1]+(a[1]-a[0]));
        }
        else{
            printf("3\n%d %d %d\n",a[0]-(a[1]-a[0]),(a[1]+a[0])/2,a[1]+(a[1]-a[0]));
        }
        return 0;
    }
    if(judge(a)){//已经是等差数列
        if(a[0]!=a[1]){
            printf("2\n");
            printf("%d %d\n",a[0]-(a[1]-a[0]),a[n-1]+(a[n-1]-a[n-2]));
        }
        else{
            printf("1\n");
            printf("%d\n",a[0]);
        }
    }
    else{
        if(!solve(a)){
            return 0*printf("0\n");
        }
        int k1=a[1]-a[0],k2=a[0];
        for(int i=1;i<n;i++){
            if(a[i]-a[i-1]>k1){
                k1=a[i]-a[i-1];
                k2=a[i-1];
            }
        }//输出
        printf("1\n");
        printf("%d\n",k2+k1/2);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值