CF1654E.Arithmetic Operations

提高+/省选-

通过率:0%

时间限制:5.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array of integers a1,a2,…,ana_1, a_2, \ldots, a_n.

You can do the following operation any number of times (possibly zero):

  • Choose any index ii and set aia_i to any integer (positive, negative or 00).

What is the minimum number of operations needed to turn aa into an arithmetic progression? The array aa is an arithmetic progression if ai+1−ai=ai−ai−1a_{i+1}-a_i=a_i-a_{i-1} for any 2≤i≤n−12 \leq i \leq n-1.

给你一个整数数组 a1,a2,…,ana_1, a_2, \ldots, a_n。

你可以执行以下操作任意次(包括零次):

  • 选择任意下标 ii,并将 aia_i 修改为任意整数(正数、负数或 00)。

将数组 aa 变为等差数列所需的最少操作次数是多少?当对任意 2≤i≤n−12 \leq i \leq n-1 都满足 ai+1−ai=ai−ai−1a_{i+1}-a_i=a_i-a_{i-1} 时,称数组 aa 是一个等差数列。

输入格式

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1051 \leq a_i \leq 10^5).

第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1051 \leq a_i \leq 10^5)。

输出格式

Print a single integer: the minimum number of operations needed to turn aa into an arithmetic progression.

输出一个整数:将 aa 变为等差数列所需的最少操作次数。

输入输出样例

  • 输入#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]a = [11, 10, 9, 8, 7, 6, 5, 4, 3] by performing 66 operations:

  • Set a3a_3 to 99: the array becomes [3,2,9,8,6,9,5,4,1][3, 2, 9, 8, 6, 9, 5, 4, 1];
  • Set a2a_2 to 1010: the array becomes [3,10,9,8,6,9,5,4,1][3, 10, 9, 8, 6, 9, 5, 4, 1];
  • Set a6a_6 to 66: the array becomes [3,10,9,8,6,6,5,4,1][3, 10, 9, 8, 6, 6, 5, 4, 1];
  • Set a9a_9 to 33: the array becomes [3,10,9,8,6,6,5,4,3][3, 10, 9, 8, 6, 6, 5, 4, 3];
  • Set a5a_5 to 77: the array becomes [3,10,9,8,7,6,5,4,3][3, 10, 9, 8, 7, 6, 5, 4, 3];
  • Set a1a_1 to 1111: the array becomes [11,10,9,8,7,6,5,4,3][11, 10, 9, 8, 7, 6, 5, 4, 3].

aa is an arithmetic progression: in fact, ai+1−ai=ai−ai−1=−1a_{i+1}-a_i=a_i-a_{i-1}=-1 for any 2≤i≤n−12 \leq i \leq n-1.

There is no sequence of less than 66 operations that makes aa 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]a = [-1, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38] by performing 1010 operations.

In the third test, you can get the array a=[100000,80000,60000,40000,20000,0,−20000,−40000,−60000,−80000]a = [100000, 80000, 60000, 40000, 20000, 0, -20000, -40000, -60000, -80000] by performing 77 operations.

在第一个测试用例中,你可以通过执行 66 次操作得到数组 a=[11,10,9,8,7,6,5,4,3]a = [11, 10, 9, 8, 7, 6, 5, 4, 3]:

  • 将 a3a_3 设为 99:数组变为 [3,2,9,8,6,9,5,4,1][3, 2, 9, 8, 6, 9, 5, 4, 1];
  • 将 a2a_2 设为 1010:数组变为 [3,10,9,8,6,9,5,4,1][3, 10, 9, 8, 6, 9, 5, 4, 1];
  • 将 a6a_6 设为 66:数组变为 [3,10,9,8,6,6,5,4,1][3, 10, 9, 8, 6, 6, 5, 4, 1];
  • 将 a9a_9 设为 33:数组变为 [3,10,9,8,6,6,5,4,3][3, 10, 9, 8, 6, 6, 5, 4, 3];
  • 将 a5a_5 设为 77:数组变为 [3,10,9,8,7,6,5,4,3][3, 10, 9, 8, 7, 6, 5, 4, 3];
  • 将 a1a_1 设为 1111:数组变为 [11,10,9,8,7,6,5,4,3][11, 10, 9, 8, 7, 6, 5, 4, 3]。

此时 aa 构成一个等差数列:事实上,对任意 2≤i≤n−12 \leq i \leq n-1,均有 ai+1−ai=ai−ai−1=−1a_{i+1}-a_i=a_i-a_{i-1}=-1。

不存在少于 66 次操作的方案能使 aa 成为等差数列。

在第二个测试用例中,你可以通过执行 1010 次操作得到数组 a=[−1,2,5,8,11,14,17,20,23,26,29,32,35,38]a = [-1, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38]。

在第三个测试用例中,你可以通过执行 77 次操作得到数组 a=[100000,80000,60000,40000,20000,0,−20000,−40000,−60000,−80000]a = [100000, 80000, 60000, 40000, 20000, 0, -20000, -40000, -60000, -80000]。

输入解题思路,AI测评打分。不知道怎么写?

首页