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 ...
随机推荐
- win10 默认锁屏路径
C:\Users\YourUsername\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\ ...
- 全文搜索(A-4)-推荐系统架构
推荐系统向用户推荐物品有三种情况. 推荐和用户已表示喜欢的物品相似的物品. 推荐和用户有相似偏好的用户喜欢的物品. 推荐包含用户偏好特征的物品.
- ajax一个很好的加载效果
推荐一个常用的jquery加载效果插件: 要引入这个插件的css和js: <link href="<%=path %>/css/showLoading.css" ...
- 概率dp呜呜
概率dp有环怎么办? 答案可劲迭代 ,然后可劲消元 , 怎么消? 我就不知道了. 呵呵
- 【Tomcat】tomcat启动后查看运行时JVM参数
Tomcat优化配置参考http://www.cnblogs.com/qlqwjy/p/8007490.html 1.启动服务后访问localhost,点击Server Status
- 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 ...
- 快速幂取模模板 && 51nod 1013 3的幂的和
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...
- Inversion 归并求逆元
bobo has a sequence a 1,a 2,…,a n. He is allowed to swap twoadjacent numbers for no more than k time ...
- C++ fill 和memset
以下内容来自www.cplusplus.com--------------------------------------------------- FILL: template <class ...
- iframe显示滚动条
子页面通过iframe加载,出现了竖向滚动条 最后查出原因:文档申明 iframe有滚动条的页面的文档申明 <!DOCTYPE html> 改成如下就行了 <!DOCTYPE HTM ...