CF978D.Almost Arithmetic Progression
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
Polycarp 喜欢等差数列。若对每个 i(1≤i<n)均有 ai+1−ai 为同一常数,则称序列 [a1,a2,…,an] 为等差数列。例如,序列 [42]、[5,5,5]、[2,11,20,29] 和 [3,2,1,0] 均为等差数列,但 [1,0,1]、[1,3,9] 和 [2,3,1] 不是。
由定义可知,任意长度为一或二的序列均为等差数列。
Polycarp 找到了一个正整数序列 [b1,b2,…,bn]。他允许对每个元素至多修改 1。换言之,对每个元素仅有三种选择:将其减 1、将其加 1,或保持不变。
请确定使序列 b 变为等差数列所需修改(恰好修改 1)的元素的最小可能个数;若不可能实现,则报告该情况。
注意:最终得到的序列中允许出现值为 0 的元素。
输入格式
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).
第一行包含一个整数 n(1≤n≤100000)—— 表示序列 b 中的元素个数。
第二行包含一个序列 b1,b2,…,bn(1≤bi≤109)。
输出格式
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).
如果无法通过所述操作构造等差数列,则输出 −1;否则,输出一个非负整数——即为使给定序列变为等差数列所需修改的最少元素个数。唯一允许的操作是将某个元素加 1 或减 1(同一位置不能进行两次操作)。
输入输出样例
输入#1
4 24 21 14 10
输出#1
3
输入#2
2 500 500
输出#2
0
输入#3
3 14 5 1
输出#3
-1
输入#4
5 1 3 6 9 12
输出#4
1
说明/提示
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.
在第一个例子中,Polycarp 应将第一个数增加 1,第二个数减少 1,第三个数增加 1,第四个数保持不变。因此,在 Polycarp 修改了三个元素(每次修改幅度为 1)后,他的序列变为 [25,20,15,10],这是一个等差数列。
在第二个例子中,Polycarp 不需要进行任何修改,因为他的序列本身就是一个等差数列。
在第三个例子中,无法将其变为等差数列。
在第四个例子中,Polycarp 只需修改第一个元素,即将其减少 1。修改后,他的序列为 [0,3,6,9,12],这是一个等差数列。
输入解题思路,AI测评打分。不知道怎么写?