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;
	}
}
发布了40 篇原创文章 · 获赞 6 · 访问量 1406

猜你喜欢

转载自blog.csdn.net/qq_43321732/article/details/104181308