D. Almost Arithmetic Progression
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp likes arithmetic progressions. A sequence [a1,a2,…,an][a1,a2,…,an] is called an arithmetic progression if for each ii (1≤i<n1≤i<n ) the value ai+1−aiai+1−ai is the same. For example, the sequences [42][42] , [5,5,5][5,5,5] , [2,11,20,29][2,11,20,29] and [3,2,1,0][3,2,1,0] are arithmetic progressions, but [1,0,1][1,0,1] , [1,3,9][1,3,9] and [2,3,1][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][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 11 , an element can be increased by 11 , an element can be left unchanged.

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

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

Input

The first line contains a single integer nn(1≤n≤100000)(1≤n≤100000) — the number of elements in bb .

The second line contains a sequence b1,b2,…,bnb1,b2,…,bn(1≤bi≤109)(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).

Examples
Input

Copy
4
24 21 14 10
Output

Copy
3
Input

Copy
2
500 500
Output

Copy
0
Input

Copy
3
14 5 1
Output

Copy
-1
Input

Copy
5
1 3 6 9 12
Output

Copy
1
Note

In the first example Polycarp should increase the first number on 11 , decrease the second number on 11 , increase the third number on 11 , and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to [25,20,15,10][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][0,3,6,9,12] , which is an arithmetic progression.

解题思路:等差数列的公差相等, 所以前两个数就可以确定公差,记录后面的数满足公差需要多少步即可,O(6 * n)的复杂度。

附ac代码:

 1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <string>
5 #include <cmath>
6 #include <string>
7 #include <iostream>
8 #include <map>
9 #include <queue>
10 #include <stack>
11 #include <cstdlib>
12 const int maxn = 3 * 1e5 + 10;
13 const int inf = 0x3f3f3f3f;
14
15 using namespace std;
16 typedef long long ll;
17 const ll mod = 1e9 + 7;
18 int nu[maxn];
19 int tem[maxn];
20 queue<int>q;
21
22 int main(int argc, const char * argv[]) {
23 int n;
24 scanf("%d", &n);
25 for(int i = 0; i < n; ++i)
26 {
27 scanf("%d", &nu[i]);
28 }
29 if(n <= 2)
30 {
31 puts("0");
32 return 0;
33 }
34 int i, j, k;
35 int ans = inf;
36 for(i = -1; i <= 1; ++i)
37 {
38 for(j = -1; j <= 1; ++j)
39 {
40 tem[0] = nu[0] + i;
41 tem[1] = nu[1] + j;
42 int d = tem[1] - tem[0];
43 int cnt = abs(i) + abs(j);
44 for(k = 2; k < n; ++k)
45 {
46 int u = nu[k] - tem[k - 1];
47 tem[k] = d + tem[k - 1];
48 if(u == d) continue;
49 if(abs(u - d) <= 1)
50 {
51 cnt++;
52 // printf("%d ", tem[k]);
53 }
54 else break;
55 }
56 if(k == n)
57 {
58 ans = min(cnt, ans);
59 }
60 }
61 }
62 if(ans == inf) puts("-1");
63 else
64 printf("%d\n", ans);
65 return 0;
66 }

最新文章

  1. IOS培训还值得么
  2. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
  3. java核心知识点学习----多线程间的数据共享的几种实现方式比较
  4. (转)mysql账号权限密码设置方法
  5. spirng MVC乱码过滤器
  6. SQL Server 查看对象之间的引用关系
  7. linux学习历程
  8. uoj #2 【NOI2014】起床困难综合症 贪心+位运算
  9. 【转】Oracle + PHP Cookbook(php oracle clob 长度超过4000如何写入)
  10. Latex 去掉行号
  11. Codefoces 723B Text Document Analysis
  12. ASP.NET MVC 中 View 的设计
  13. Python Socket第二篇(socketserver)
  14. hadoop学习大纲
  15. RocketMQ源码 — 七、 RocketMQ高可用(2)
  16. [POJ 1637] Sightseeing tour(网络流)
  17. 织梦dedecms安全设置详情
  18. maven使用出现的错误
  19. Array.prototype.removeBeginWithVal(删除数组内以某值开头的字符串对象)
  20. 从0开始接触html--第一天学习内容总结

热门文章

  1. oracle 12C单实例打PSU
  2. SAP 摘录数据集
  3. unstable sort
  4. (Oracle)DDL及其数据泵导入导出(impdp/expdp)
  5. shiro的授权与认证
  6. BZOJ1031
  7. 2020最新idea永久激活教程
  8. SparkStreaming直连方式读取kafka数据,使用MySQL保存偏移量
  9. centos编译安装vim7.4
  10. Kubernetes --(k8s)入门