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 (1i<n1≤i<n) the value ai+1aiai+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 n(1n100000)(1≤n≤100000) — the number of elements in bb.

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

 

枚举 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/465953.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Sourcetail 一款代码编辑神器,让看源码如丝般顺滑

大家好&#xff0c;我是小麦&#xff0c;上一次的文章《一个超级实用的源码阅读小技巧》&#xff0c;我简单提到通过gccegyptgraphviz(dot)这三个工具&#xff0c;快速生成函数调用图&#xff0c;总体来说也非常好用。众所周知VSCode&#xff0c;Sorceinsight非常好用&#xff…

CPU占用率是什么?

这是昨晚上在朋友圈发的一个讨论回答的问题很多因为这个问题&#xff0c;我晚上也没早睡&#xff0c;就想把这个讲清楚说这个问题之前我们先了解下时间片调度算法&#xff0c;因为用这个来讲比较清楚。CPU可以执行任务&#xff0c;CPU给哪个进程执行&#xff0c;哪个进程就可以…

每日一题(17)—— 关键字volatile的作用和三个不同的应用场合

关键字volatile的作用和三个不同的应用场合 作用&#xff1a;防止编译器优化&#xff1b; &#xff08;1&#xff09;并行设备的硬件寄存器&#xff08;如状态寄存器&#xff09;&#xff1b;// #define S5PV210_GPIO_DATA (*(volatile unsigned char *)0x40000000) &#x…

维护邮件系统

维护邮件系统转载于:https://blog.51cto.com/february/195437

长大了,也弄丢了很多东西

小明要回家处理父亲的头七&#xff0c;那天晚上跟他喝酒聊天&#xff0c;我们说对生活的一些计划&#xff0c;比如计划让小孩上哪所小学&#xff0c;比如上小学了上哪所初中&#xff0c;高中&#xff0c;报什么课外兴趣班等等。吃过苦的孩子心里都有一种执念&#xff0c;想让自…

我的爹,我的娘(2006-02-22 21:39:07)(新浪)

爸爸妈妈是农民&#xff08;我也是&#xff09;&#xff0c;都已年过半百。他们很忙碌&#xff0c;闲不下来.爸爸头发这几年越来越少了&#xff0c;头发也白了不少&#xff0c;但坚持经常染发&#xff0c;因为他说&#xff0c;怕我的小兄弟产生为什么别人爸爸这么年轻的想法。我…

每日一题(18)—— 指定地址存入数据

往内存地址0x1c1b上存入一个数据0x10&#xff0c;使用指针如何操作&#xff0c;写出代码。 *((volatile unsigned int *)0x1c1b) 0x10;

.Net序列化与反序列化的2种方法

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; //方法一的引用 using System.Web.Script.Serialization; //方法二的引用namespace ConsoleApplication1 {class Program…

200个模块,怎么用有线的方式进行组网通信

这是一个读者私聊我的问题&#xff0c;我觉得比较有意思&#xff0c;发出来大家一起讨论&#xff0c;我相信在讨论后&#xff0c;会有更加好的的方案。问题有200个模块&#xff0c;这200个模块需要和主CPU进行通信&#xff0c;每个模块的距离大概在30~50cm之间&#xff0c;用怎…

两招让陌生网络访问者现出原形

当我们关闭计算机时&#xff0c;经常出现“有一台计算机与你的电脑有连接,如果关闭将中断数据”的提示&#xff0c;尤其是在局域网中这种提示更是经常出现。其实这是因为别人通过网络正在访问自己电脑上的共享资源呢!面对如此恶劣的行动&#xff0c;难道只能任其胡作非为吗!当然…

将二维数组名作为函数实参

可以用二维数组名作为实参或者形参&#xff0c;在被调用函数中对形参数组定义时可以指定所有维数的大小&#xff0c;也可以省略第一维的大小说明&#xff0c;如&#xff1a; void Func(int array[3][10]); void Func(int array[][10]); 二者都是合法而且等价&#xff0c;但是不…

每日一题(19)—— 用变量a给出下面的定义

(a) 一个整型数&#xff08;An integer&#xff09; (b) 一个指向整型数的指针&#xff08;A pointer to an integer&#xff09; (c) 一个指向指针的的指针&#xff0c;它指向的指针是指向一个整型数&#xff08;A pointer to a pointer to an integer&#xff09; (d) 一个…

网易创始人之一,周卓林先生

这篇文章的主人公我觉得对我们做技术的人非常有借鉴作用。大学的时候&#xff0c;我们老师就一直强调&#xff0c;一命&#xff0c;二运&#xff0c;三风水&#xff0c;四积德&#xff0c;五读书&#xff0c;后面我就不记得了。1、1997年2月&#xff0c;周卓林上Hotmail&#x…

sql2005 无法解决 equal to 操作Chinese_PRC_CI_AS 和 Chinese_PRC_CS_AS 之间的排序

错误代码&#xff1a; SELECT * FROM SJCCBZ, Order_Motor WHERE SJCCBZ.OrderIDOrder_Motor.OrderID and SJCCBZ.OrderID1AND Order_Motor.M_State已报账 错误提示&#xff1a; 消息 468&#xff0c;级别 16&#xff0c;状态 9&#xff0c;第 1 行 无法解决 equal to 操作中 …

MapReduce编程中常用的字符操作

本文主要用于记录自己在编写mapreduce程序时常用的一些方法&#xff0c;后期会不断更新&#xff0c;用于自己复习和给新手一些帮助。 字符串操作 String str " 12345"; // 字符串中是否包含某子字符串System.out.println(str.contains("123"));// …

用单片机制作简单的频率计

点击上方“果果小师弟”&#xff0c;选择“置顶/星标公众号”干货福利&#xff0c;第一时间送达&#xff01;摘要&#xff1a;利用定时器产生PWM波。然后利用32的外部中断和定时器来测量32输出的波形 硬件&#xff1a;STM32F103C8T6核心板、示波器、串口调试助手 所用到的的引脚…

每日一题(20)——malloc与free(一)

void GetMemory(char *p) {p (char *)malloc(100); }void Test(void) {char *str NULL;GetMemory(str);strcpy(str, "hello world");printf(str); } 1、指出编程错误 2、指出错误后果 3、指出纠正方法 分析&#xff1a; 1、调用GetMemory( str )后&#xff0c;…

gentoo ffmpeg+mencoder快速搭建视频处理系统笔记

软件说明&#xff1a;MP3支持&#xff1a;lamexvid支持&#xff1a;xvidH264/AVC支持&#xff1a;x264AC3支持&#xff1a;liba52MPEG4 ACC支持&#xff1a;faac、faad23GPP AMR Floating point 和 AMR-Wideband支持&#xff1a;libamrnb、libamrwbemerge lame libogg libvorbi…

深入理解Linux内核之内核抢占

1.开场白环境&#xff1a;处理器架构&#xff1a;arm64内核源码&#xff1a;linux-5.11ubuntu版本&#xff1a;20.04.1代码阅读工具&#xff1a;vimctagscscope我们或许经常听说过内核抢占&#xff0c;可是我们是否真正理解它呢&#xff1f;内核抢占和抢占式内核究竟有什么关系…

Python+Selenium学习笔记10 - send_keys上传文件

在火狐浏览器上传文件 上传前&#xff0c;同一个HTML文件在火狐和Edge浏览器显示有些不同 这是Firefox浏览器的显示 这是Edge浏览器 上传后 1 # coding utf-82 3 from selenium import webdriver4 import os5 import time6 7 dr webdriver.Firefox()8 file_path "file:…