CodeForces 382C Arithmetic Progression (排序+分类讨论)
题意:给出一个长度为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 (排序+分类讨论)的更多相关文章
- Codeforces 685C - Optimal Point(分类讨论+乱搞)
Codeforces 题面传送门 & 洛谷题面传送门 分类讨论神题. 首先看到最大值最小,一眼二分答案,于是问题转化为判定性问题,即是否 \(\exists x_0,y_0,z_0\) 满足 ...
- Codeforces 1513F - Swapping Problem(分类讨论+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 简单题,难度 *2500 的 D2F,就当调节一下一模炸裂了的自闭的心情,稍微写写吧. 首先我看到这题的第一反应是分类讨论+数据结构,即枚 ...
- Codeforces 1461F - Mathematical Expression(分类讨论+找性质+dp)
现场 1 小时 44 分钟过掉此题,祭之 大力分类讨论. 如果 \(|s|=1\),那么显然所有位置都只能填上这个字符,因为你只能这么填. scanf("%d",&n);m ...
- codeforces C. Arithmetic Progression 解题报告
题目链接:http://codeforces.com/problemset/problem/382/C 题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能 ...
- Codeforces 870F - Path(数论+分类讨论+正难则反)
Codeforces 题目传送门 & 洛谷题目传送门 首先考虑 \(d(u,v)\) 是个什么东西,分情况讨论: \(u\not\perp v\),\(d(u,v)=1\) \(u\perp ...
- Codeforces 1114E - Arithmetic Progression - [二分+随机数]
题目链接:http://codeforces.com/problemset/problem/1114/E 题意: 交互题,有一个 $n$ 个整数的打乱顺序后的等差数列 $a[1 \sim n]$,保证 ...
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
- CodeForces - 1221E Game With String 分类讨论
首先分析A能获胜的情况 A能获胜 当且仅当A拿完后所有剩下的都<b 所以一旦存在一个大小为X的 且 b<=X<a 则必是后手赢 当X为 a<=x<2*b 的时候 无论A或 ...
- CodeForces 792C - Divide by Three [ 分类讨论 ]
删除最少的数位和前缀0,使得剩下的数能被3整除 等价于各数位数字之和能被3整除. 当前数位和可能是 0, 1, 2(mod 3) 0: 直接处理 1: 删除一个a[i]%3 == 1 或者 两个a[i ...
随机推荐
- Uva 12657 移动盒子(双向链表)
题意: 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.可以执行以下4种指令:1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y右 ...
- Spring之HelloWorld
[Spring是什么?] 1.Spring是一个开源框架. 2.Spring为简化企业级应用开发而生,使用Spring可以使简单的JavaBean实现以前只有EJB(EJB是sun的JavaEE服务器 ...
- 添物不花钱学JavaEE(基础篇)-JSP
JSP(JavaServer Pages)是做Java Web开发必须掌握的语言. JSP: JavaServer Pages is a technology for developing web p ...
- 饭卡-HDU2546(01背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory L ...
- Eclipse-Java代码规范和质量检查插件-PMD
PMD是一个源代码分析器. 它发现常见的编程缺陷,如未使用的变量.空catch块.不必要的对象创建等等. 它支持Java.JavaScript.Salesforce.com Apex.PLSQL.Ap ...
- Eclipse-Java代码规范和质量检查插件-阿里编码规约
此工具配套阿里巴巴Java开发手册:http://www.cnblogs.com/EasonJim/p/6436387.html Eclipse安装和使用方法:https://github.com/a ...
- 在gentoo中打开tomcat的远程调试开关
在一般象gentoo等发行版中,系统安装tomcat这类软件后会产生一些启动脚本, 比如是/etc/init.d/tomcat-7, 启动方式与原始的tomcat不太一样.在gentoo中,假设须要远 ...
- 用pc构建DIY计算集群
-----------------------------------------------------------------用pc构建DIY计算集群目录/构建计算集群|-- /0前言|-- /1 ...
- 赵雅智_Android的getResources()资源引用
今天做一个Android的刮刮乐项目.里面用到非常多的地方用到了getResources. <span style="font-size:12px;"> // 获得图片 ...
- python 区块链程序
python 区块链程序 学习了:https://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247484921&idx=1& ...