当前位置:网站首页>1700C - Helping the Nature
1700C - Helping the Nature
2022-07-06 10:01:00 【Stellaris_L】
题目
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LPovsZs2-1656470420013)(C:/Users/李俊霖/AppData/Roaming/Typora/typora-user-images/image-20220629102534925.png)]](/img/ce/49712244660d81007040c2ab42c5bf.png)
题意
给一个数组,存在以下三种操作,
- (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;
}
边栏推荐
- How to submit data through post
- RB157-ASEMI整流桥RB157
- SAP UI5 框架的 manifest.json
- Grafana 9 is officially released, which is easier to use and more cool!
- Optimization of middle alignment of loading style of device player in easycvr electronic map
- 一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
- sql语句优化,order by desc速度优化
- 传统家装有落差,VR全景家装让你体验新房落成效果
- Smart street lamp based on stm32+ Huawei cloud IOT design
- Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
猜你喜欢

Reppoints: advanced order of deformable convolution

scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月

OliveTin能在网页上安全运行shell命令(上)

Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing

基于STM32+华为云IOT设计的智能路灯

FlutterWeb瀏覽器刷新後無法回退的解决方案

面试突击63:MySQL 中如何去重?

10 advanced concepts that must be understood in learning SQL

在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
![[introduction to MySQL] third, common data types in MySQL](/img/11/66b4908ed8f253d599942f35bde96a.png)
[introduction to MySQL] third, common data types in MySQL
随机推荐
FlutterWeb浏览器刷新后无法回退的解决方案
学 SQL 必须了解的 10 个高级概念
In terms of byte measurement with an annual salary of 30W, automated testing can be learned in this way
Smart street lamp based on stm32+ Huawei cloud IOT design
BearPi-HM_ Nano development board "flower protector" case
中移动、蚂蚁、顺丰、兴盛优选技术专家,带你了解架构稳定性保障
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
Remote code execution penetration test - B module test
How to submit data through post
Debug xv6
Concept and basic knowledge of network layering
scratch疫情隔离和核酸检测模拟 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
遠程代碼執行滲透測試——B模塊測試
BearPi-HM_ Nano development environment
It doesn't make sense without a distributed gateway
Unity tips - draw aiming Center
Kali2021 installation and basic configuration
Openharmony developer documentation open source project
酷雷曼多种AI数字人形象,打造科技感VR虚拟展厅