题意:给出一个长度为n的序列,表示有n张卡片,上面的数字,现在还有一张卡片,上面没有数字,问说可以写几种数字在这张卡片上面,

使得n+1张卡片上的数字可以排列成一个等差数列,有无限多种时输出-1.

析:首先排序是肯定的,然后再分成几种,如果只有一个数,那么就一定是-1,如果是两个数时,在前面和后面一定可以加一个,这个也要注意相等的情况,

然后再考虑中间的情况,如果它们的绝对差是偶数,那么中间也可以再放一个,再就是大于等于3个数时候,这个也要考虑是不是全相等,然后再考虑这个

序列是不是可以加一个数成一个等差,再考虑前面和后面。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #define freopenr freopen("in.txt", "r", stdin)
  17. #define freopenw freopen("out.txt", "w", stdout)
  18. using namespace std;
  19. typedef long long LL;
  20. typedef pair<int, int> P;
  21. const int INF = 0x3f3f3f3f;
  22. const double inf = 0x3f3f3f3f3f3f;
  23. const LL LNF = 0x3f3f3f3f3f3f;
  24. const double PI = acos(-1.0);
  25. const double eps = 1e-8;
  26. const int maxn = 1e5 + 100;
  27. const int mod = 1e9 + 7;
  28. const int dr[] = {-1, 0, 1, 0};
  29. const int dc[] = {0, 1, 0, -1};
  30. const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  31. int n, m;
  32. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  33. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  34. inline int Min(int a, int b){ return a < b ? a : b; }
  35. inline int Max(int a, int b){ return a > b ? a : b; }
  36. inline LL Min(LL a, LL b){ return a < b ? a : b; }
  37. inline LL Max(LL a, LL b){ return a > b ? a : b; }
  38. inline int gcd(int a, int b){ return b ? gcd(b, a%b) : a; }
  39. inline int lcm(int a, int b){ return a * b / gcd(a, b); }
  40. inline bool is_in(int r, int c){
  41. return r >= 0 && r < n && c >= 0 && c < m;
  42. }
  43. int a[maxn];
  44.  
  45. int main(){
  46. while(scanf("%d", &n) == 1){
  47. for(int i = 0; i < n; ++i) scanf("%d", a+i);
  48. sort(a, a+n);
  49. if(1 == n) printf("-1\n");
  50. else if(n == 2){
  51. int k = a[1] - a[0];
  52. if(!k) printf("1\n%d\n", a[0]);
  53. else if(k&1){
  54. printf("2\n");
  55. printf("%d %d\n", a[0]-k, a[1]+k);
  56. }
  57. else{
  58. printf("3\n");
  59. printf("%d %d %d\n", a[0]-k, a[0]+k/2, a[1]+k);
  60. }
  61. }
  62. else{
  63. int cnt = 0, k = INF;
  64. for(int i = 1; i < n; ++i)
  65. k = Min(k, a[i]-a[i-1]);
  66. bool ok = true, flag = false;
  67. for(int i = 1; i < n; ++i){
  68. if(k == a[i]-a[i-1]) continue;
  69. if(flag){ ok = false; break; }
  70. else if(k * 2 == a[i]-a[i-1]){ flag = true, cnt = a[i-1] + k; }
  71. else { ok = false; break; }
  72. }
  73.  
  74. if(!ok) printf("0\n");
  75. else if(!k) printf("1\n%d\n", a[0]);
  76. else if(flag) printf("1\n%d\n", cnt);
  77. else{
  78. printf("2\n");
  79. printf("%d %d\n", a[0]-k, a[n-1]+k);
  80. }
  81. }
  82. }
  83. return 0;
  84. }

CodeForces 382C Arithmetic Progression (排序+分类讨论)的更多相关文章

  1. Codeforces 685C - Optimal Point(分类讨论+乱搞)

    Codeforces 题面传送门 & 洛谷题面传送门 分类讨论神题. 首先看到最大值最小,一眼二分答案,于是问题转化为判定性问题,即是否 \(\exists x_0,y_0,z_0\) 满足 ...

  2. Codeforces 1513F - Swapping Problem(分类讨论+乱搞)

    Codeforces 题目传送门 & 洛谷题目传送门 简单题,难度 *2500 的 D2F,就当调节一下一模炸裂了的自闭的心情,稍微写写吧. 首先我看到这题的第一反应是分类讨论+数据结构,即枚 ...

  3. Codeforces 1461F - Mathematical Expression(分类讨论+找性质+dp)

    现场 1 小时 44 分钟过掉此题,祭之 大力分类讨论. 如果 \(|s|=1\),那么显然所有位置都只能填上这个字符,因为你只能这么填. scanf("%d",&n);m ...

  4. codeforces C. Arithmetic Progression 解题报告

    题目链接:http://codeforces.com/problemset/problem/382/C 题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能 ...

  5. Codeforces 870F - Path(数论+分类讨论+正难则反)

    Codeforces 题目传送门 & 洛谷题目传送门 首先考虑 \(d(u,v)\) 是个什么东西,分情况讨论: \(u\not\perp v\),\(d(u,v)=1\) \(u\perp ...

  6. Codeforces 1114E - Arithmetic Progression - [二分+随机数]

    题目链接:http://codeforces.com/problemset/problem/1114/E 题意: 交互题,有一个 $n$ 个整数的打乱顺序后的等差数列 $a[1 \sim n]$,保证 ...

  7. ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...

  8. CodeForces - 1221E Game With String 分类讨论

    首先分析A能获胜的情况 A能获胜 当且仅当A拿完后所有剩下的都<b 所以一旦存在一个大小为X的 且 b<=X<a 则必是后手赢 当X为 a<=x<2*b 的时候 无论A或 ...

  9. CodeForces 792C - Divide by Three [ 分类讨论 ]

    删除最少的数位和前缀0,使得剩下的数能被3整除 等价于各数位数字之和能被3整除. 当前数位和可能是 0, 1, 2(mod 3) 0: 直接处理 1: 删除一个a[i]%3 == 1 或者 两个a[i ...

随机推荐

  1. win10 默认锁屏路径

    C:\Users\YourUsername\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\ ...

  2. 全文搜索(A-4)-推荐系统架构

    推荐系统向用户推荐物品有三种情况. 推荐和用户已表示喜欢的物品相似的物品. 推荐和用户有相似偏好的用户喜欢的物品. 推荐包含用户偏好特征的物品.

  3. ajax一个很好的加载效果

    推荐一个常用的jquery加载效果插件: 要引入这个插件的css和js: <link href="<%=path %>/css/showLoading.css" ...

  4. 概率dp呜呜

    概率dp有环怎么办? 答案可劲迭代 ,然后可劲消元 , 怎么消? 我就不知道了. 呵呵

  5. 【Tomcat】tomcat启动后查看运行时JVM参数

    Tomcat优化配置参考http://www.cnblogs.com/qlqwjy/p/8007490.html 1.启动服务后访问localhost,点击Server Status

  6. Educational Codeforces Round 45 (Rated for Div. 2) C、D

      C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 ...

  7. 快速幂取模模板 && 51nod 1013 3的幂的和

    #include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...

  8. Inversion 归并求逆元

    bobo has a sequence a 1,a 2,…,a n. He is allowed to swap twoadjacent numbers for no more than k time ...

  9. C++ fill 和memset

    以下内容来自www.cplusplus.com--------------------------------------------------- FILL: template <class ...

  10. iframe显示滚动条

    子页面通过iframe加载,出现了竖向滚动条 最后查出原因:文档申明 iframe有滚动条的页面的文档申明 <!DOCTYPE html> 改成如下就行了 <!DOCTYPE HTM ...