CF1654E.Arithmetic Operations
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array of integers a1,a2,…,an .
You can do the following operation any number of times (possibly zero):
- Choose any index i and set ai to any integer (positive, negative or 0 ).
What is the minimum number of operations needed to turn a into an arithmetic progression? The array a is an arithmetic progression if ai+1−ai=ai−ai−1 for any 2≤i≤n−1 .
输入格式
The first line contains a single integer n ( 1≤n≤105 ).
The second line contains n integers a1,a2,…,an ( 1≤ai≤105 ).
输出格式
Print a single integer: the minimum number of operations needed to turn a into an arithmetic progression.
输入输出样例
输入#1
9 3 2 7 8 6 9 5 4 1
输出#1
6
输入#2
14 19 2 15 8 9 14 17 13 4 14 4 11 15 7
输出#2
10
输入#3
10 100000 1 60000 2 20000 4 8 16 32 64
输出#3
7
输入#4
4 10000 20000 10000 1
输出#4
2
说明/提示
In the first test, you can get the array a=[11,10,9,8,7,6,5,4,3] by performing 6 operations:
- Set a3 to 9 : the array becomes [3,2,9,8,6,9,5,4,1] ;
- Set a2 to 10 : the array becomes [3,10,9,8,6,9,5,4,1] ;
- Set a6 to 6 : the array becomes [3,10,9,8,6,6,5,4,1] ;
- Set a9 to 3 : the array becomes [3,10,9,8,6,6,5,4,3] ;
- Set a5 to 7 : the array becomes [3,10,9,8,7,6,5,4,3] ;
- Set a1 to 11 : the array becomes [11,10,9,8,7,6,5,4,3] .
a is an arithmetic progression: in fact, ai+1−ai=ai−ai−1=−1 for any 2≤i≤n−1 .
There is no sequence of less than 6 operations that makes a an arithmetic progression.
In the second test, you can get the array a=[−1,2,5,8,11,14,17,20,23,26,29,32,35,38] by performing 10 operations.
In the third test, you can get the array a=[100000,80000,60000,40000,20000,0,−20000,−40000,−60000,−80000] by performing 7 operations.