1453B Suffix Operations

本文探讨了一个有趣的问题:如何通过最少的操作使数组中的所有元素相等。允许的操作包括增加或减少数组后缀的值,以及更改数组中任一整数的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目
Gildong has an interesting machine that has an array a with n integers. The machine supports two kinds of operations:

1.Increase all elements of a suffix of the array by 1.
2.Decrease all elements of a suffix of the array by 1.
A suffix is a subsegment (contiguous elements) of the array that contains an. In other words, for all i where ai is included in the subsegment, all aj’s where i<j≤n must also be included in the subsegment.

Gildong wants to make all elements of a equal — he will always do so using the minimum number of operations necessary. To make his life even easier, before Gildong starts using the machine, you have the option of changing one of the integers in the array to any other integer. You are allowed to leave the array unchanged. You want to minimize the number of operations Gildong performs. With your help, what is the minimum number of operations Gildong will perform?

Note that even if you change one of the integers in the array, you should not count that as one of the operations because Gildong did not perform it.

Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤1000).

Each test case contains two lines. The first line of each test case consists of an integer n (2≤n≤2⋅105) — the number of elements of the array a.

The second line of each test case contains n integers. The i-th integer is ai (−5⋅108≤ai≤5⋅108).

It is guaranteed that the sum of n in all test cases does not exceed 2⋅105.

Output
For each test case, print one integer — the minimum number of operations Gildong has to perform in order to make all elements of the array equal.

Example
input

7
2
1 1
3
-1 0 2
4
99 96 97 95
4
-3 -5 -2 1
6
1 4 3 2 4 1
5
5 0 0 0 5
9
-367741579 319422997 -415264583 -125558838 -300860379 420848004 294512916 -383235489 425814447

output

0
1
3
4
6
5
2847372102

Note
In the first case, all elements of the array are already equal. Therefore, we do not change any integer and Gildong will perform zero operations.

In the second case, we can set a3 to be 0, so that the array becomes [−1,0,0]. Now Gildong can use the 2-nd operation once on the suffix starting at a2, which means a2 and a3 are decreased by 1, making all elements of the array −1.

In the third case, we can set a1 to 96, so that the array becomes [96,96,97,95]. Now Gildong needs to:

Use the 2-nd operation on the suffix starting at a3 once, making the array [96,96,96,94].
Use the 1-st operation on the suffix starting at a4 2 times, making the array [96,96,96,96].
In the fourth case, we can change the array into [−3,−3,−2,1]. Now Gildong needs to:

Use the 2-nd operation on the suffix starting at a4 3 times, making the array [−3,−3,−2,−2].
Use the 2-nd operation on the suffix starting at a3 once, making the array [−3,−3,−3,−3].

题意: 给定一个长度为n的整形数组a,我们可以进行两种操作,1.选取任意后缀子串所有数字加1;2.选取任意后缀子串所有数字减1。除此之外,我们拥有一次将其中一个整数改成任意一个数的机会。我们要求将所有数字变成相等的最小操作数。
思路: 思维题,因为只能改变后缀,要将所有数变得相等,先不考虑最后一个条件,每次只能将a[i+1]变成和a[i]相等(后面的数也会变化a[i]-a[i+1]),那么这样总操作数sum就是每两个数字差的绝对值。我们还有一个条件没有用。改变一个数字,那么改变哪个数字呢?改变的数肯定是变成和相邻的数相等,我们要找出来贡献最大的那个数。用sum-贡献值最大的数即可。
贡献最大的数的找法:举个简单的例子,a[0],a[1],a[2]三个数,a[1]变成a[0],那么操作数就是abs(a[2]-a[0]),原本的操作数是abs(a[1]-a[0])+abs(a[2]-a[1]),两者的差值就是贡献值。遍历找最大的即可。特判首尾的情况。
详细见代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#define ll long long
using namespace std;
int n;
ll a[200005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)scanf("%lld",&a[i]);
        ll maxn=abs(a[0]-a[1]),sum=0;
        for(int i=0;i<n-1;i++){
            sum+=abs(a[i]-a[i+1]);
            if(i!=0)maxn=max(abs(a[i]-a[i-1])+abs(a[i]-a[i+1])-abs(a[i-1]-a[i+1]),maxn);
        }
        /*单独考虑第一个变成第二个和倒数第二个变成最后一个的情况*/
        maxn=max(abs(a[1]-a[0]),maxn);
        maxn=max(abs(a[n-1]-a[n-2]),maxn);
        cout<<sum-maxn<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a碟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值