当前位置:网站首页>C. Helping the nature (CF) difference
C. Helping the nature (CF) difference
2022-06-22 00:05:00 【Xuanji, you have no heart】
Original link :Problem - C - Codeforces
The main idea of the topic : Give you an array a, The range of each number is −1e9≤ai≤1e9, Each operation can be selected from the following operations :
1) hold a1 ... ai Subtract one from all the numbers
2) hold ai ... an Subtract one from all the numbers
3) hold a1...an Add one to all the numbers in the whole array
Ask you the minimum number of operations so that each element in the entire array is 0?
Ideas :
One segment is subtracted / Add operation ----> Difference
So by the a[i] have to b[i] Array ( The difference between every two adjacent elements ), Because all numbers will eventually become 0, therefore b[] The array should also be all 0, So it can be used 1 or 2 operation , From back to front b[] All become 0, After each change ,b[i] After that b[j] All for 0 了 , That's the array a[] It will all be the same ; Note that if b[i] here >=0, So it is to subtract the following part 1, So don't worry about ; But if b[i]<0, Is to start from a[1] To a[i] All minus 1, therefore a[1] Will change , Here we need to change a[1]. Finally, put b[n] To b[2] It's over , Look again a[1] And 0 The difference between , It can be used separately 2 and 3 The operation adds or subtracts the entire array to 0!
AC Code :
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
#define PII pair<int,int>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define rrep(i, n) for(int i = n; i >= 1; ++i)
using namespace std;
const double pi = acos(-1.0);
const int N = 2e5 + 10;
ll a[N], b[N];
int main()
{
int t, n;
cin >> t;
while(t--)
{
cin >> n;
ll res = 0;
rep(i, n) cin >> a[i], b[i] = a[i] - a[i - 1];
for(int i = n; i > 1; i--)
{
if(b[i] >= 0) res += b[i];
else res -= b[i], b[1] += b[i];
}
res += abs(b[1]);
cout << res << endl;
}
return 0;
}
边栏推荐
- 学生管理系统实验报告-asp.net程序设计
- Taishan Office Technology Lecture: Microsoft YaHei font intentionally set the pit, bold error
- Redis master-slave replication (9)
- eureka的解析
- Inventaire des exploits courants
- Xiuno Shura light forum imitation Zhihu blue simple responsive theme template 1.7+ adaptive pc+wap terminal
- ERP已死,管理后台已凉,秒杀系统称王!
- 你有一个机会,这里有一个舞台
- Online text batch inversion by line tool
- Programming dry goods │ PHP common method encapsulation
猜你喜欢

Must the database primary key be self incremented? What scenarios do not suggest self augmentation?

pytorch可视化

基于Arduino框架下VSCode PlatformIO一个项目配置两种不同开发板的兼容模式

Kirin System Development Notes (V): making and installing the startup USB flash disk of the Kirin system, installing the Kirin system on the physical machine, and building a Qt development environment

What if the program input point cannot be located in the dynamic link library

7. target detection
![[Database Course Design] classroom information management system based on SQL Server (with part of source code)](/img/7e/47011ee2a35c50669a86fd5cf543d9.png)
[Database Course Design] classroom information management system based on SQL Server (with part of source code)

Unity network development (II)

CVPR2022 | 弱监督多标签分类中的损失问题
![class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist](/img/1a/294eb0128285686ede415991f69be7.png)
class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist
随机推荐
Création de mono
Inventaire des exploits courants
关于 SecureFx传输远程服务器中文显示乱码 的解决方法
盤點常見的漏洞利用方式
微博关闭发布多个兼职诈骗信息违规账号:如何打击数据造假灰产
编程干货│PHP 常见方法封装
Academician Zhang Jun: the latest paper on unmanned intelligence group and its social integration, Journal of the Chinese Academy of Engineering
leetcode1337. Row K with the weakest combat effectiveness in the matrix
Must the database primary key be self incremented? What scenarios do not suggest self augmentation?
无法定位程序输入点于动态链接库怎么办
Layout roadmap, the perfect combination of spatial layout and data visualization
关于 QtCreator的设计器QtDesigner完全无法正常拽托控件 的解决方法
RK3568开发笔记(三):RK3568虚拟机基础环境搭建之更新源、安装网络工具、串口调试、网络连接、文件传输、安装vscode和samba共享服务
discuz!论坛修复站帮网vip插件bug:VIP会员到期后,重新开通永久会员时,所属的用户组没有切换到永久会员分组
Is Oriental Fortune futures regular? Is this platform safe and reliable?
Today's sleep quality record 81 points
Hardware development notes (IV): basic process of hardware development, making a USB to RS232 module (III): design schematic diagram
Win11打字不显示选字框怎么办?Win11打字不显示选字框的解决方法
学生管理系统实验报告-asp.net程序设计
Isn't the so-called 0 copy just to let the CPU rest? Deep understanding of MMAP