[关闭]
@Chilling 2017-02-16T10:56:07.000000Z 字数 2630 阅读 847

CodeForces-382C: Arithmetic Progression(模拟)


Description

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

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个数,判断再加入一个数能不能使这n+1个数成为等差数列,如果可以,问加入的数字有多少种可能,输出有几种可能,并且按从小到大的顺序输出他们。如果有无数种可能,输出-1。

分析:首先按照从小到大的顺序对这些数字排序。后面就是……惊天大模拟
注意:若相邻两个数的差有2个不同的,其中一个差必定只出现一次,并且是更大的。如:1,2,4,5,差1出现2次,差2出现一次,添加3就可以构成等差数列,而2,4,5,7中,1出现1次,2出现两次,不能添加一个数字构成等差数列。


  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<map>
  4. #include<string.h>
  5. #define LL long long
  6. using namespace std;
  7. LL a[100005];
  8. LL b[100005];
  9. int main()
  10. {
  11. int n;
  12. while(scanf("%d",&n)!=EOF)
  13. {
  14. map<LL,LL>m;
  15. map<LL,LL>::iterator it;
  16. memset(b,0,sizeof(b));
  17. for(int i=0;i<n;i++)
  18. scanf("%lld",&a[i]);
  19. if(n==1)
  20. {
  21. printf("-1\n");
  22. continue;
  23. }
  24. sort(a,a+n);
  25. for(int i=n-1;i-1>=0;i--)
  26. {
  27. int j=i;
  28. m[a[i]-a[i-1]]++;
  29. b[j]=a[i]-a[i-1];
  30. }
  31. if(m.size()>2)
  32. printf("0\n");
  33. else
  34. {
  35. if(m.size()==1)
  36. {
  37. LL x,y;
  38. for(it=m.begin();it!=m.end();it++)
  39. x=it->first,y=it->second;
  40. if(x==0)
  41. printf("1\n%lld\n",a[0]);
  42. else if((x%2==1)||y>1)
  43. printf("2\n%lld %lld\n",a[0]-x,a[n-1]+x);
  44. else
  45. printf("3\n%lld %lld %lld\n",a[0]-x,(a[0]+a[n-1])/2,a[n-1]+x);
  46. }
  47. else
  48. {
  49. LL x[3],y[3],j=0;
  50. LL xx=-1;
  51. for(it=m.begin();it!=m.end();it++)
  52. {
  53. x[j]=it->first;
  54. y[j++]=it->second;
  55. }
  56. if(y[0]!=1&&y[1]!=1)
  57. printf("0\n");
  58. else
  59. {
  60. if((x[0]>x[1]&&y[0]>y[1])||(x[0]<x[1]&&y[0]<y[1]))
  61. printf("0\n");
  62. else
  63. {
  64. if(y[0]<y[1])
  65. {
  66. if(x[0]==2*x[1])
  67. xx=x[1];
  68. else
  69. printf("0\n");
  70. }
  71. if(y[0]>y[1])
  72. {
  73. if(2*x[0]==x[1])
  74. xx=x[0];
  75. else
  76. printf("0\n");
  77. }
  78. else
  79. {
  80. if(x[0]==2*x[1])
  81. xx=x[1];
  82. if(x[1]==2*x[0])
  83. xx=x[0];
  84. else
  85. printf("0\n");
  86. }
  87. if(xx!=-1)
  88. {
  89. int aa;
  90. for(aa=0;aa<n;aa++)
  91. {
  92. if(b[aa]==2*xx)
  93. break;
  94. }
  95. printf("1\n%d\n",a[aa]-xx);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. return 0;
  103. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注