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

424 21 14 10

output

Copy

3

input

Copy

2500 500

output

Copy

0

input

Copy

314 5 1

output

Copy

-1

input

Copy

51 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.

枚举 a[1]、a[2]的所有情况,一但a[1]-a[2]确定,则所有数之间的差确定,看修改元素是否能达到这种可能。如果可以,维护一个最小值。

#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
int main()
{int n,i,j,k;int a[maxn]={0};int b[maxn]={0};scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&a[i]);}if(n==2||n==1){printf("0\n");return 0;}int minum=10000000;int flag=0;for(i=-1;i<=1;i++){for(j=-1;j<=1;j++){int cnt=abs(i)+abs(j);for(k=1;k<=n;k++){b[k]=a[k];}b[1]+=i;b[2]+=j;int x=b[1]-b[2];for(k=2;k<=n-1;k++){if(b[k]-(b[k+1]+1)==x){b[k+1]+=1;cnt++;}else if(b[k]-b[k+1]==x){}else if(b[k]-(b[k+1]-1)==x){cnt++;b[k+1]-=1;}else{break;}if(k==n-1){minum=min(minum,cnt);flag=1;}}}}if(flag) printf("%d\n",minum);else printf("-1\n");return 0;
}

  

转载于:https://www.cnblogs.com/zyf3855923/p/9039766.html

Almost Arithmetic Progression相关推荐

  1. Almost Arithmetic Progression(CF-978D)

    Problem Description Polycarp likes arithmetic progressions. A sequence [a1,a2,-,an] is called an ari ...

  2. 奇思妙想构造题 ARC145 D - Non Arithmetic Progression Set

    Non Arithmetic Progression Set 大意: 给定m,n 要求构造一个序列: 长度=n,元素和=m,任意三个元素无法构成等差数列,|a[i]|<=1e7 思路: 一开始这 ...

  3. cf----2019-10-20(Consecutive Subsequence,Almost Arithmetic Progression,Mentors)

    一场游戏一场空,最终 最初都由我掌控,好像一身从容,不曾有狼狈伤痛,可深夜一个人该如何相拥? You are given an integer array of length nn. You have ...

  4. PAT(甲级)2021年春季考试 7-1 Arithmetic Progression of Primes

    思路:用筛除法打素数表(与之相对的是枚举加逐个判断)是降低时间复杂度的第一个点,第二个点是运用上数学技巧,给定了等差数列的范围(2-MAX),给定了个数,那么最大的等差是可以求出的.循环的第一层从最大 ...

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

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

  6. Arithmetic Progression 题解(随机数使用)

    思路: 本题的意思:求首项与公差. 第一部分:观察一下,题目的第二类操作(比某数大,有单调性)明显是要我们用二分(或者我觉得其实随机数也行,根据随机数来不断缩小范围,但是这题练二分)求最大值,大概要操 ...

  7. HDU5142 NPY and arithmetic progression BestCoder Round #23 1002

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 解题思路:BestCoder官方题解: 可以发现等差数列只有(123,234,1234和长度&g ...

  8. CF1673DLost Arithmetic Progression 题解

    a,b公差的最小公倍数就是c的公差 判断0的情况比较多,但是都比较trival 1. 区间c未完全属于区间a 2.公差不能除尽 3.首项之差不能除尽a的公差,简而言之就是对不上 判断-1的情况我考场的 ...

  9. 2021牛客多校第二场 A——Arithmetic Progression

    题目大意 给你一个长度为 nnn 的数列 aaa ,数列中每个元素都不一样,问你存在多少个区间,这些区间内的数排序后是一个等差数列 解题思路 对于一个区间,如果这个区间内的数排序后的元素可以构成一个等 ...

最新文章

  1. 微信小程序开发简易计算器改进版
  2. 如何在MyEclipse中将项目部署Tomcat
  3. [@Controller]4 详解@ModelAttribute
  4. 人工智能秘史(二):美国第一台计算机背后的女程序员
  5. JAVA与SAP数据交互的方式总结
  6. 一天就能打印一栋房子超大型3D打印机
  7. 黑苹果16g内存够用吗_现阶段最便宜的完美黑苹果配置是什么?
  8. Promise的deferred对象详解
  9. python tornade 模板扩展
  10. php随机生成卡密,PHP随机生成不反复的8位卡号(数字)和卡密(字符串)_后端开发...
  11. MATLAB线条颜色
  12. JQUI dialog中使用datepicker
  13. 【SDOI2015】寻宝游戏
  14. 如何给php安装上pecl,PHP PECL如何安装扩展?
  15. bom成本分析模型_拆解苹果HomePod发现BoM成本高达248.4美元
  16. BFT-SMaRt:用Netty做客户端的可靠信道
  17. 数据库——ODBC连接
  18. PAT.A1010 Radix
  19. 实验四-哈夫曼编码的MATLAB实现
  20. java 容器排序_Java攻略第四章 容器类、排序

热门文章

  1. org-mode入门教程
  2. 基于SuperSocket的IIS主动推送消息给android客户端
  3. 《离散数学》双语专业词汇表 名词术语中英文索引
  4. E24- please install the following Perl modules before executing ./mysql_install_db
  5. makefile——小试牛刀
  6. div 超出高度滚动条,超出宽度点点点
  7. [原创]windows server 2012 AD架构试验系列 – 12 配置操作主机
  8. MySQL Cluster 用户权限共享 (各sql节点同步)
  9. Android应用开发—PendingIntent:如何判断两个PendingIntent对等
  10. 【狂神说】分析前后端分离开源项目?