当前位置:网站首页>1700C - Helping the Nature
1700C - Helping the Nature
2022-07-06 10:01:00 【Stellaris_L】
题目
题意
给一个数组,存在以下三种操作,
- (1)将数组中 a 1 ∼ a i a_1\sim a_i a1∼ai 的数减一。
- (2)将数组中 a i ∼ a n a_i\sim a_n ai∼an 的数减一。
- (3)将数组中全部数加一。
求将数组全部数字变为 0 的最小操作数。
使用差分数组,将原数组变为全零转化为将差分数组变为全零。可以将原来的操作进行转化。
- (1) b 1 − 1 b_1-1 b1−1, b i + 1 b_i+1 bi+1。
- (2) b i − 1 b_i-1 bi−1。
- (3) b 1 + 1 b_1+1 b1+1。
所以,发现只有操作(1)能对除 1 以外的数字加一,只有操作(2)能对除 1 以外位置减一,所以只需要对差分数组除 1 以外的位置进行处理,最后用(2)(3)操作判断位置 1 就可以。
注意要开 long long。
#include <iostream>
using namespace std;
typedef long long ll;
const int N=2e5+10;
ll T,a[N],b[N];
void slove(){
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)b[i]=a[i]-a[i-1];
ll ans=0;
for(int i=2;i<=n;i++){
if(b[i]>0)ans+=b[i];
else if(b[i]<0){
ans-=b[i];
b[1]+=b[i];
}
}
cout<<ans+abs(b[1])<<endl;
}
int main(){
cin>>T;while(T--)
slove();
return 0;
}
边栏推荐
- MySQL stored procedure
- Reppoints: advanced order of deformable convolution
- Hongmeng introduction and development environment construction
- [introduction to MySQL] the first sentence · first time in the "database" Mainland
- Spark calculation operator and some small details in liunx
- Smart street lamp based on stm32+ Huawei cloud IOT design
- Xin'an Second Edition: Chapter 12 network security audit technology principle and application learning notes
- 【MySQL入门】第一话 · 初入“数据库”大陆
- 历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
- What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
猜你喜欢
Pytest learning ----- pytest confitest of interface automation test Py file details
【MySQL入门】第一话 · 初入“数据库”大陆
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
F200——搭载基于模型设计的国产开源飞控系统无人机
Summary of Android interview questions of Dachang in 2022 (II) (including answers)
[translation] principle analysis of X Window Manager (I)
Spark accumulator and broadcast variables and beginners of sparksql
传统家装有落差,VR全景家装让你体验新房落成效果
Reppoints: advanced order of deformable convolution
SAP UI5 框架的 manifest.json
随机推荐
MySQL 8 sub database and table backup database shell script
Shell input a string of numbers to determine whether it is a mobile phone number
远程代码执行渗透测试——B模块测试
微信小程序获取手机号
Xin'an Second Edition: Chapter 12 network security audit technology principle and application learning notes
Getting started with pytest ----- allow generate report
Optimization of middle alignment of loading style of device player in easycvr electronic map
The most complete tcpdump and Wireshark packet capturing practice in the whole network
编译原理——预测表C语言实现
How to use scroll bars to dynamically adjust parameters in opencv
EasyCVR授权到期页面无法登录,该如何解决?
Getting started with pytest ----- test case rules
Smart street lamp based on stm32+ Huawei cloud IOT design
[elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias
Openharmony developer documentation open source project
The art of Engineering (1): try to package things that do not need to be exposed
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
Summary of Android interview questions of Dachang in 2022 (I) (including answers)
The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
Quick start of Hongmeng system