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

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

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

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

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

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 100;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline int gcd(int a, int b){ return b ? gcd(b, a%b) : a; }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn]; int main(){
while(scanf("%d", &n) == 1){
for(int i = 0; i < n; ++i) scanf("%d", a+i);
sort(a, a+n);
if(1 == n) printf("-1\n");
else if(n == 2){
int k = a[1] - a[0];
if(!k) printf("1\n%d\n", a[0]);
else if(k&1){
printf("2\n");
printf("%d %d\n", a[0]-k, a[1]+k);
}
else{
printf("3\n");
printf("%d %d %d\n", a[0]-k, a[0]+k/2, a[1]+k);
}
}
else{
int cnt = 0, k = INF;
for(int i = 1; i < n; ++i)
k = Min(k, a[i]-a[i-1]);
bool ok = true, flag = false;
for(int i = 1; i < n; ++i){
if(k == a[i]-a[i-1]) continue;
if(flag){ ok = false; break; }
else if(k * 2 == a[i]-a[i-1]){ flag = true, cnt = a[i-1] + k; }
else { ok = false; break; }
} if(!ok) printf("0\n");
else if(!k) printf("1\n%d\n", a[0]);
else if(flag) printf("1\n%d\n", cnt);
else{
printf("2\n");
printf("%d %d\n", a[0]-k, a[n-1]+k);
}
}
}
return 0;
}

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. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  2. 杭电 2037 今年暑假不AC

    Problem Description “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACM ...

  3. Lucene_Hello(示例)

    (1)创建project (2)导入Lucene的核心包 (3)编写代码建立索引 /lucene01/src/cn/hk/lucene/TestIndex.java: package cn.hk.lu ...

  4. caca需要用到x11作为图形输出

    编译错误:no output drivers were selected!. yum -y install xcb-proto yum -y install libxcb-devel.x86_64 l ...

  5. [HDU2196]Computer(DP)

    传送门 题意 给出一棵树,求离每个节点最远的点的距离 思路 对于我这种菜鸡,真是难啊. 每个点的距离它最远的点,除了在它子树中的,还有在它子树之外的,所以这几个状态都得表示出来. 我们能够很简单的求出 ...

  6. Eclipse配置SVN的几种方法及使用详情

    此文章对Myeclipse同样适用. 一.在Eclipse里下载Subclipse插件 方法一:从Eclipse Marketplace里面下载 具体操作:打开Eclipse --> Help ...

  7. Servlet处理日期

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/handling-date.html: 使用Servlet的最重要的优势之一是可以使用核心Java ...

  8. laravel event

    事件监听 方法一: web.php Event::listen('eloquent.created: App\post',function(){ dump('A post was created'); ...

  9. 基于Wi-Fi的HID注射器,利用WHID攻击实验

    WHID代表基于 Wi-Fi 的 HID 注射器,即对 HID 攻击进行无线化攻击的一种注入工具. 实验攻击原理如下图: 攻击者使用ESP8266作为AP,在自己的电脑创建客户端连接AP.在客户端键入 ...

  10. HDU 5265 pog loves szh II (二分查找)

    [题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...