Problem Description

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 bb 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≤10^9).

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

4
24 21 14 10

Output

3

Input

2
500 500

Output

0

Input

3
14 5 1

Output

-1

Input

5
1 3 6 9 12

Output

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

题意: 给出长为 n 的数列,可对每一个数进行 +1,-1,不变的操作,求最少要几次操作能使这个数列变成等差数列,如果不能变为等差数列,则输出 -1

思路:由于只要确定了前两个数的差,就可以确定后面所有的数了,所以,直接枚举前两个数的差,然后检查后面的数是否符合要求,然后暴力枚举即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 1000005
#define MOD 1e9+7
#define E 1e-6
#define LL long long
using namespace std;
int a[N],b[N];
int main()
{int n;cin>>n;for(int i=1;i<=n;i++)cin>>a[i];int minn=INF;for(int i=-1;i<=1;i++)//共3*3=9种情况{for(int j=-1;j<=1;j++){bool flag=true;int ans=abs(i)+abs(j);b[1]=a[1]+i;b[2]=a[2]+j;int d=b[2]-b[1];for(int k=3;k<=n;k++){if( abs( (b[k-1]+d)-a[k] )<=1 ){b[k]=b[k-1]+d;ans+=abs(b[k]-a[k]);}else{flag=false;break;}}if(flag)minn=min(minn,ans);}}if(minn!=INF)cout<<minn<<endl;elsecout<<-1<<endl;return 0;
}

Almost Arithmetic Progression(CF-978D)相关推荐

  1. 【解题报告】随便练练二(CF 2300)

    [解题报告]随便练练二(CF 2300) A:Antimatter | CF383D 题意 思路 :DP 代码 B:Physical Education Lessons | CF915E 题意 思路一 ...

  2. Commentator problem(CF 2)

    题目链接 题目大意: 给定三个圆,询问是否存在点满足该点与三个圆夹角均相等,若存在多组解返回夹角最大值. 圆外一点到两圆夹角均相等: 即 sina = sinb = r1 / d1 = r2 / d2 ...

  3. Codeforces Round #224 (Div. 2): C. Arithmetic Progression(模拟)

    题意: 给你n个数字,你需要再添加一个数字,使得最后所有数字排序之后任意相邻两个数之差全部相等,问可以添加多少种不同的数字 思路: 一看就是水题但是情况不少,没了 例如所有数字全部相等,只有两个数字, ...

  4. Arithmetic Sharing(算术共享)

    Sharing Senantics Sharing Values. For an kkk-bit Arithmetic sharing xxx of x, we have x0+x1=x_0+x_1= ...

  5. D. Make a Power of Two(cf#739DIV3)

    D. Make a Power of Two 链接: link. 题意: 找出将数字转换为 2 的任意幂的最小移动次数. 题解: 先将2的xxx次幂的结果以字符串形式保存,输入字符串nnn后,因为存在 ...

  6. Web of Lies(CF 1548A)

    这是今天在打个人赛时碰见的一道题,是一道半图论半思维的题. Web of Lies 题目大意不难理解,在这里只需要注意一些细节.在加边时,只有当cnt[min]的值为1时答案才应该减1,而不是当cnt ...

  7. Magic Powder - 2 (CF 670_D)

    http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...

  8. 【解题报告】博弈专场 (CF 2000~2200)前五题

    [解题报告]博弈专场 (CF 2000+)前五题 A:Fox and Card Game | CF388C 题意 思路 代码 B:Berzerk | CF786A 题意 思路 代码 C:Ithea P ...

  9. 软件设计师提纲+复习资料整理(上午题)

    文章目录 软件设计师考试大纲 上午题(选择题) 一.计算机组成原理 考点:CPU结构组成 考点:原码.反码.补码定点整数范围 考点:浮点数表示 考点:RISC和CISC计算机的区别 考点:奇校验与偶校 ...

最新文章

  1. python中如何对复杂的json数据快速查找key对应的value值(使用JsonSearch包)
  2. zcu102 hdmi example(一)
  3. oracle分区表编程,Oracle分区表详解
  4. NIFI使用过程中的invalid component问题解决
  5. python获取网页数据对电脑性能_【Python】网页数据爬取实战
  6. Elasticsearch的javaAPI之facet,count,delete by query
  7. 能力提升综合题单Part 8.9.3 费用流
  8. 程序员必看!java开发金融类项目
  9. 计算机时钟电路检查,数字电子时钟电路设计实训报告
  10. js数据在html乱码,通过网页查看JS源码中汉字显示乱码的解决方法
  11. 【手写源码-设计模式7】-桥接模式-基于苹果小米手机刷机场景
  12. java se检查异常,JavaSE:异常(下)
  13. Android实现欢迎界面
  14. 计算机网络密码凭据,win7系统共享提示输入网络凭据用户名密码的解决办法
  15. 关于“类包含显式重写ctor”的解决方法
  16. eplan好看的电缆图表_eplan,电缆图表,模板
  17. 情商比智商更能决定人的一生
  18. extern的几种用法
  19. 常用数据挖掘工具简介
  20. C#实现改变图片的像素

热门文章

  1. 人口简史:我们曾经差一点就彻底没了
  2. hive 配置mysql_Hive的mysql安装配置
  3. 亚马逊涨了 $4 千亿?!为什么它能成为疫情中获益最大的公司
  4. Istio微服务平台集成实践
  5. JEECG_3.7 新版本视频正式发布
  6. 【JEECG技术文档】online自定义模板的使用
  7. 微信开放平台 公众号第三方平台开发 教程五 代公众号发起网页授权源码
  8. mysql中json_extract函数的使用?作用是什么?
  9. URL(待整合到HTTP书中哦)
  10. Windows API(非MFC)编程加载位图