CodeForces - 978D(Almost Arithmetic Progression)

题目:
Polycarp likes arithmetic progressions. A sequence [a1,a2,…,an] is called an arithmetic progression if for each i (1≤i<n) the value ai+1−ai is the same. For example, the sequences [42], [5,5,5], [2,11,20,29] and [3,2,1,0] are arithmetic progressions, but [1,0,1], [1,3,9] and [2,3,1] are not.

It follows from the definition that any sequence of length one or two is an arithmetic progression.

Polycarp found some sequence of positive integers [b1,b2,…,bn]. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by 1, an element can be increased by 1, an element can be left unchanged.

Determine a minimum possible number of elements in b which can be changed (by exactly one), so that the sequence b becomes an arithmetic progression, or report that it is impossible.

It is possible that the resulting sequence contains element equals 0.

Input
The first line contains a single integer n (1≤n≤100000) — the number of elements in b.
The second line contains a sequence b1,b2,…,bn (1≤bi≤109).
Output
If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can’t use operation twice to the same position).
样例

输入:
4
24 21 14 10
输出:
3
输入:
2
500 500
输出:
0
输入:
3
14 5 1
输出:
-1

Note:
In the first example Polycarp should increase the first number on 1, decrease the second number on 1, increase the third number on 1, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to [25,20,15,10], which is an arithmetic progression.

In the second example Polycarp should not change anything, because his sequence is an arithmetic progression.

In the third example it is impossible to make an arithmetic progression.

In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like [0,3,6,9,12], which is an arithmetic progression.

题意:
给你一组数,然后给你三种操作,分别是+1,-1,不变,然后判断能否通过操作将这组数变成等差数列,若能则输出最小改变次数,若不行则输出-1。
思路:
乍一看题,以为是DFS的题,使劲想怎么搜,后来搜超时了555。下面说正解,其实根据等差数列的性质,只需要处理前两项枚举所有可能结果,暴力找出最小次数即可。

AC代码:

#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = 100000 +50;
const int INF = 0x3f3f3f3f;
long long a[maxn], b[maxn];
int main()
{
	int n;
	while (cin >> n) {
		for (int i = 0; i < n; i++)
			cin >> a[i];
		int num = INF;
		for (int i = -1; i <= 1; i++) {
			for (int j = -1; j <= 1; j++) {
				b[0] = a[0] + i;
				b[1] = a[1] + j;
				int d = b[1] - b[0];
				int flag = 0;
				int t = abs(i) + abs(j);
				for (int r = 2; r < n; r++) {
					b[r] = b[r - 1] + d;
					if (abs(b[r] - a[r]) > 1) {
						flag = 1;
						break;
					}
					else {
						if (b[r] != a[r])
							t++;
					}
				}
				if (flag == 0)
					num = min(num, t);
			}
		}
		if (num == INF)
			cout << "-1" << endl;
		else
			cout << num << endl;
	}
}
全部评论

相关推荐

10-20 15:26
门头沟学院 Java
桥头牛油火锅:这个比例不正常,简历的话项目经历放中间,项目功能分点可以再明确点,前面加“·”或者“1 2 3”,另外简历上的照片可以去外面摄影店拍一下,以后也会用到的,hr筛人也是多少会看的,毕竟世界是一个巨大的卡颜局嘛,还有有些hr由于消息太多可能没看到,后面可能会回来找你,要简历的还会多一点,我也是普2本,比例大致是600:90:15:3,当然我实力不太够,拿的offer比较少,慢慢来吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务