1453B Suffix Operations

题目
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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
智慧校园2.0是高校信息化建设的新阶段,它面对着外部环境变化和内生动力的双重影响。国家战略要求和信息技术的快速发展,如云计算、大数据、物联网等,为智慧校园建设提供了机遇,同时也带来了挑战。智慧校园2.0强调以服务至上的办学理念,推动了教育模式的创新,并对传统人才培养模式产生了重大影响。 智慧校园建设的解决之道是构建一个开放、共享的信息化生态系统,利用互联网思维,打造柔性灵活的基础设施和强大的基础服务能力。这种生态系统支持快速迭代的开发和持续运营交付能力,同时注重用户体验,推动服务创新和管理变革。智慧校园的核心思想是“大平台+微应用+开放生态”,通过解耦、重构和统一运维监控,实现服务复用和深度融合,促进业务的快速迭代和自我演化。 智慧校园的总体框架包括多端协同,即“端”,它强调以人为中心,全面感知和捕获行为数据。这涉及到智能感知设备、超级APP、校园融合门户等,实现一“码”或“脸”通行,提供线上线下服务端的无缝连接。此外,中台战略是智慧校园建设的关键,包括业务中台和数据中台,它们支持教育资源域、教学服务域等多个领域,实现业务的深度融合和数据的全面治理。 在技术层面,智慧校园的建设需要分期进行,逐步解耦应用,优先发展轻量级应用,并逐步覆盖更多业务场景。技术升级路径包括业务数据化、数据业务化、校园设施智联化等,利用IoT/5G等技术实现设备的泛在互联,并通过人工智能与物联网技术的结合,建设智联网。这将有助于实现线上线下一网通办,提升校园安全和学习生活体验,同时支持人才培养改革和后勤管理的精细化。 智慧校园的建设不仅仅是技术的升级,更是对教育模式和管理方式的全面革新。通过构建开放、共享的信息化生态系统,智慧校园能够更好地适应快速变化的教育需求,提供更加个性化和高效的服务,推动教育创新和人才培养的高质量发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

a碟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值