Codeforces 382 C. Arithmetic Progression
2014-01-18 02:00
543 查看
先由小到大排序
1:只有一个数 无限多
2:存在3种以上的间距 无
3:出现2种间距
大的间距只有1个,且长度是小的两倍 1种
大的间距有几个 无
4:只有1种间距
只出现一次 一头一尾 或者 加上中间
**仅有的间距是0 1种
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).
Sample test(s)
input
output
input
output
input
output
input
output
input
output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
int n,ct=0;
int a[200200];
set<int> vis;
struct node
{
int dist,num,pos;
}D[5];
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
}
if(n==1)
{
printf("-1\n"); return 0;
}
sort(a,a+n);
bool flag=true;
for(int i=1;i<n;i++)
{
int dd=a[i]-a[i-1];
if(vis.count(dd)==0)
{
vis.insert(dd);
D[ct].dist=dd;
D[ct].num=1;
D[ct].pos=i;
ct++;
}
else
{
for(int j=0;j<ct;j++)
{
if(D[j].dist==dd)
{
D[j].num++;
break;
}
}
}
if(ct>=3)
{
flag=false; break;
}
}
if(flag==false)
{
printf("0\n"); return 0;
}
if(ct==1&&D[0].num!=1)
{
int Dd=D[0].dist;
if(Dd!=0)
{
printf("2\n");
printf("%d %d\n",a[0]-Dd,a[n-1]+Dd);
}
else
{
printf("1\n");
printf("%d\n",a[0]);
}
return 0;
}
if(ct==1&&D[0].num==1)
{
int Dd=D[0].dist;
if(Dd)
{
if(Dd%2==0)
{
printf("3\n");
printf("%d %d %d\n",a[0]-Dd,a[0]+Dd/2,a[n-1]+Dd);
}
else
{
printf("2\n");
int Dd=D[0].dist;
printf("%d %d\n",a[0]-Dd,a[n-1]+Dd);
}
return 0;
}
else
{
printf("1\n");
printf("%d\n",a[0]);
return 0;
}
}
int dade=(D[0].dist>D[1].dist)? 0 : 1;
if(D[dade].num!=1)
{
printf("0\n"); return 0;
}
else
{
if(D[dade].dist!=D[1-dade].dist*2)
{
printf("0\n"); return 0;
}
}
printf("1\n");
printf("%d\n",a[D[dade].pos-1]+(a[D[dade].pos]-a[D[dade].pos-1])/2);
return 0;
}
1:只有一个数 无限多
2:存在3种以上的间距 无
3:出现2种间距
大的间距只有1个,且长度是小的两倍 1种
大的间距有几个 无
4:只有1种间距
只出现一次 一头一尾 或者 加上中间
**仅有的间距是0 1种
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).
Sample test(s)
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
int n,ct=0;
int a[200200];
set<int> vis;
struct node
{
int dist,num,pos;
}D[5];
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
}
if(n==1)
{
printf("-1\n"); return 0;
}
sort(a,a+n);
bool flag=true;
for(int i=1;i<n;i++)
{
int dd=a[i]-a[i-1];
if(vis.count(dd)==0)
{
vis.insert(dd);
D[ct].dist=dd;
D[ct].num=1;
D[ct].pos=i;
ct++;
}
else
{
for(int j=0;j<ct;j++)
{
if(D[j].dist==dd)
{
D[j].num++;
break;
}
}
}
if(ct>=3)
{
flag=false; break;
}
}
if(flag==false)
{
printf("0\n"); return 0;
}
if(ct==1&&D[0].num!=1)
{
int Dd=D[0].dist;
if(Dd!=0)
{
printf("2\n");
printf("%d %d\n",a[0]-Dd,a[n-1]+Dd);
}
else
{
printf("1\n");
printf("%d\n",a[0]);
}
return 0;
}
if(ct==1&&D[0].num==1)
{
int Dd=D[0].dist;
if(Dd)
{
if(Dd%2==0)
{
printf("3\n");
printf("%d %d %d\n",a[0]-Dd,a[0]+Dd/2,a[n-1]+Dd);
}
else
{
printf("2\n");
int Dd=D[0].dist;
printf("%d %d\n",a[0]-Dd,a[n-1]+Dd);
}
return 0;
}
else
{
printf("1\n");
printf("%d\n",a[0]);
return 0;
}
}
int dade=(D[0].dist>D[1].dist)? 0 : 1;
if(D[dade].num!=1)
{
printf("0\n"); return 0;
}
else
{
if(D[dade].dist!=D[1-dade].dist*2)
{
printf("0\n"); return 0;
}
}
printf("1\n");
printf("%d\n",a[D[dade].pos-1]+(a[D[dade].pos]-a[D[dade].pos-1])/2);
return 0;
}
相关文章推荐
- Codeforces 382 D Permutations
- CodeForces 382 B.Number Busters(鸽巢定理)
- codeforces 382 B Number Busters(推公式)
- Codeforces 382 B. Number Busters
- Codeforces 382 B. Number Busters(数论推公式)
- CodeForces 382 E.Ksenia and Combinatorics(组合数学+dp)
- CodeForces 353B Two Heaps
- CodeForces 176B Word Cut dp
- CodeForces 540A Combination Lock
- 【Codeforces 760 B Frodo and pillows】+ 二分
- codeforces 731A Night at the Museum
- CodeForces 171 B.Star(水~)
- CodeForces - 483B Friends and Presents
- Codeforces - 337C(div2) - Harmony Analysis
- CodeForces 289A Polo the Penguin and Segments (水题)
- Codeforces 762A k-th divisor
- CodeForces - 831C: Jury Marks(前缀和去重、STL)
- D - Funky Numbers (CodeForces - 192A)
- Codeforces 目录
- codeforces 620E. New Year Tree dfs序+线段树+bitset