题目链接:http://codeforces.com/problemset/problem/382/C

题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能的情况,并输出这些数。

n = 1 、 n = 2 和 整个序列是常数列 的情况比较容易判断。不过要注意n = 2的时候,也需要判断两个数之间是否也可以通过插入一个数来构成等差数列。

关键是讨论n>=3的情况。预处理:把整个输入序列从小到大排序。之后,得到公差是第一要务!如果可以从中插入一个数(这时一定不是两端,也就是说这两种情况是互斥的),那么两个相邻的数之差 = 公差的次数会是最多的!只要找到这个差出现不少于2次以上,这个差就是公差。

确定公差之后,后面的情况就比较容易判断。插入该数的两个相邻数之间的差不可能等于公差,而该数是否合法,可以通过这两个相邻数中较小的一个 + 2倍公差,能否得到较大的数来判断。

如果插入的数不在中间,那么有可能是无解(输出0,插入哪个位置都不能使得序列成为等差数列)或者是从两端插入(输入的序列已经是等差数列)

还有一种情况要特别注意,当序列只有3个数的时候,并且可以从中插入一个数成为等差数列的情况。此时公差取较小的那个差,因为较大的差之间比较小的差能插入该数的可能性更大。例如序列:1 3 4,我们会取1作为公差,而不是2,此时可在1和3之间插入2来构成等差数列。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; const int maxn = 1e5 + ;
int a[maxn]; int main()
{
int i, cnt, n, d, ans, td, td1, c1, c2;
while (scanf("%d", &n) != EOF)
{
for (i = ; i <= n; i++)
scanf("%d", &a[i]);
sort(a+, a+n+);
d = a[]-a[];
if (n == ) // 可以插入无限个数,只要等于a[1]就行
printf("-1\n");
else if (a[] == a[n] && n >= ) // 常数列
printf("1\n%d\n", a[]);
else if (n == )
{
if ((a[] + a[]) % ) // 判断相邻数之间是否也可以插入
printf("2\n%d %d\n", a[]-d, a[]+d);
else
printf("3\n%d %d %d\n", a[]-d, (a[]+a[])/, a[]+d);
}
else
{
if (n >= )
{
c2 = c1 = ;
td1 = a[]-a[];
if (td1 != d)
{
for (i = ; i <= n; i++)
{
int td = a[i] - a[i-];
// 出现次数>=2的差即为整个序列的公差 if (td == td1)
c2++;
else
c1++;
if (c1 >= || c2 >= )
break;
}
}
if (c2 >= c1 && n >= && d > td1) // 最终确定公差
d = td1;
}
cnt = ;
for (i = ; i <= n; i++)
{
td = a[i] - a[i-];
if (d != td)
{
cnt++;
if ((a[i-]+a[i]) % || a[i-]+*d != a[i])
{
cnt++;
break;
}
else
ans = a[i-] + d;
}
}
if (!cnt)
printf("2\n%d %d\n", a[]-d, a[n]+d);
else if (cnt == )
printf("1\n%d\n", ans);
else
printf("0\n");
}
}
return ;
}

codeforces C. Arithmetic Progression 解题报告的更多相关文章

  1. Codeforces Round 665 赛后解题报告(暂A-D)

    Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...

  2. Codeforces Round 662 赛后解题报告(A-E2)

    Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...

  3. USACO Section1.4 Arithmetic Progressions 解题报告

    ariprog解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...

  4. Codeforces Round #277.5 解题报告

    又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...

  5. codeforces B. Simple Molecules 解题报告

    题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...

  6. codeforces A. IQ Test 解题报告

    题目链接:http://codeforces.com/problemset/problem/328/A 一开始单纯地直接判断给出的序列是等差还是等比,连这一句“You should also prin ...

  7. codeforces 591A. Wizards' Duel 解题报告

    题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be ...

  8. codeforces 582A. GCD Table 解题报告

    题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...

  9. codeforces 581C. Developing Skills 解题报告

    题目链接:http://codeforces.com/problemset/problem/581/C 题目意思:给出 n 个数:a1, a2, ..., an (0 ≤ ai ≤ 100).给出值 ...

随机推荐

  1. 【SDOI2008】解题汇总

    好叭我真的是闲的了... /---------------------------------------------/ BZOJ-2037 [SDOI2008]Sue的小球 DP+相关费用提前计算 ...

  2. HDU1907 John

    Description Little John is playing very funny game with his younger brother. There is one big box fi ...

  3. CodeForces 559C Gerald and Giant Chess

    C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. php5.5安装及phpmyadmin&nginx配置php模块

    安装php5.5: 下载源地址:rpm -Uvh rpm包安装:yum install php55w.x86_64 php55w-cli.x86_64 php55w-common.x86_64 php ...

  5. Linux创建线程

    #include"stdio.h" #include"pthread.h" #include"unistd.h" ; void *creat ...

  6. CSS 仿Excel表格功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. jgestures 一款jquery手势插件

    jgestures插件允许你如同原生的jQuery事件一样监听以下事件:'pinch'(缩放手势), 'rotate'(旋转手势), 'swipe'(滑动手势), 'tap'(轻触) 以及 'orie ...

  8. WPF PopupNonTopmost重写

    之前做WPF遇到问题,在网上找到的一个类 public class PopupNonTopmost : System.Windows.Controls.Primitives.Popup { publi ...

  9. iOS-(kCFStreamErrorDomainSSL, -9802)

    kCFStreamErrorDomainSSL, -9802 我是微博授权时get页面时候碰到的 其实就是http安全问题 在info.plist里添加并设置Allow Arbitrary Loads ...

  10. (转) 新的开始之Win7、CentOS 6.4 双系统 硬盘安装

    http://blog.csdn.net/cnclenovo/article/details/11358447