CodeForces - 382C Arithmetic Progression 思维

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
题意:有一些数,你要加上一个数使之成为等差序列,求所有答案。
思路: 一个分类讨论的题...
当n==1 时可以添加无限多个 输出-1
当n==2 时 首先可以在首尾各填一个,还有就是当这个公差d为偶数时 可以在两个数中间填一个
当n==3 时 两个公差d1 d2 如果相等 首尾各填一个, 当d1=2*d2 时 1 2间填一个 d2=2*d1 2 3 填一个 其余的都不可以了
当n>3 时 因为只能填一个数 我们最多只能有两种公差,所以我们要记录公差 并且还要记录大公差出现的次数不能大于一
一个公差 首尾一个
两个公差 大公差出现次数不能超过一次 否则肯定不可以,而且大公差和小公差之间还是要存在2倍的关系

其余的全部不满足题意;
插的数可能会相同...而且要排序 所以用了set

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
int a[maxn],d[maxn];
int main()
{
set<int>st;
int n;
cin>>n;
st.clear();
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n);
if(n==1)
{
printf("-1\n");
return 0;
}
if(n==2)
{
d[0]=a[2]-a[1];
st.insert(a[2]+d[0]);
st.insert(a[1]-d[0]);
if(d[0]%2==0)
st.insert(a[1]+d[0]/2);
}
else if(n==3)
{
d[0]=a[2]-a[1];
d[1]=a[3]-a[2];
if(d[0]==d[1])
{
st.insert(a[1]-d[0]);
st.insert(a[3]+d[0]);
}
if(d[0]==2*d[1])
{
st.insert(a[1]+d[1]);
}
if(d[1]==2*d[0])
{
st.insert(a[2]+d[0]);
}
}
else
{
int k=0,count=0;
map<int ,int >mp;
for(int i=2;i<=n;i++)
{
k=a[i]-a[i-1];
mp[k]++;
if(mp[k]==1)
{
d[count++]=k;
if(count>=3)
break;
}
}
if(count==1)
{
st.insert(a[1]-d[0]);
st.insert(a[n]+d[0]);
}
else if(count==2)
{
if(mp[d[0]]==1&&d[0]==2*d[1])
{
for(int i=2;i<=n;i++)
{
if(a[i]-a[i-1]==d[0])
{
st.insert(a[i]-d[1]);
break;
}
}

}
if(mp[d[1]]==1&&d[1]==2*d[0])
{
for(int i=2;i<=n;i++)
{
if(a[i]-a[i-1]==d[1])
{
st.insert(a[i]-d[0]);
break;
}
}
}
}

}
bool flag=false;
printf("%d\n",st.size());
set<int>::iterator it;
for(it=st.begin();it!=st.end();it++)
{
if(flag)
printf(" ");
printf("%d",*it);
flag=true;
}
if(st.size())
printf("\n");
return 0;
}




阅读更多
版权声明:~~~~博主新博客https://www.statusrank.xyz/ https://blog.csdn.net/HowardEmily/article/details/60572813
个人分类: codeforces 思维
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭