当前位置:网站首页>C. Helping the Nature(cf)差分
C. Helping the Nature(cf)差分
2022-06-21 17:55:00 【璇玑你没有心】
题目大意:给你一个数组a,其中每个数的范围是−1e9≤ai≤1e9,每一次操作可以在以下操作中选择:
1)把a1 ... ai所有数减一
2) 把ai ... an所有数减一
3)把a1...an整个数组中所有数加一
问你最少操作次数使得整个数组中每个元素都为0?
思路:
一段进行减操作/加操作 ----> 差分
所以由a[i]得b[i]数组(每两个相邻的元素之间的差值),因为所有数最后都要变成0,所以b[]数组也应该全为0,所以可以用1或2操作,从后往前把b[]全变为0,每次变完,b[i]之后的b[j]都为0了,也就是数组a[]后面都会变成一样的了;注意如果b[i]此时>=0,那么就是将后面部分减1,所以不用管;但是如果b[i]<0,就是把从a[1]到a[i]全部减1,所以a[1]会变,这里要改变a[1]。最后把b[n]到b[2]变完了,再看a[1]与0的差别,可以分别用2和3操作把整个数组全加或全减变为0!
AC代码:
#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;
}
边栏推荐
- Second cloud's original fully compatible solution for Xinchuang is upgraded to help accelerate the implementation of Xinchuang industry
- Nebula Graph入驻阿里云计算巢,助力企业打造云上超大规模图数据库
- Two ways of encrypting Excel files
- 2022中国眼博会,山东青少年眼睛健康展,视力矫正与康复展
- The dist function of R language calculates the distance between two samples in dataframe data and returns the distance matrix between samples. The distance matrix is input to the hclust function for h
- 企评家全面解读:【国家电网】中国电力财务有限公司企业成长性
- Image classification, AI and automatic performance test
- El expression
- Leetcode (210) - Schedule II
- R语言使用epiDisplay包的statStack函数基于因子变量通过分层的方式查看连续变量的统计量(均值、中位数等)以及对应的假设检验
猜你喜欢
随机推荐
MySQL的MVCC实现原理
2022年6月25日PMP考试通关宝典-5
El expression
文件上传漏洞靶场分析 UPLOAD_LABS
近年区域赛(20-22)
Foreign capital staged a "successful flight" and domestic capital took over the offer. Is New Oriental online "square"?
[HCTF 2018]WarmUp
缓存设计问题
根据数据中的key获取value值
JDBC编程六步
Literature analysis CiteSpace 6.1.2 download and installation tutorial
36氪首发 | 聚焦健康险产品创新,「英仕健康」已获4轮融资
Two problems that may occur in the use of ThreadLocal and thread pool
空中操作仅通过距离映射对遮挡目标进行鲁棒定位(RAL2022)
Yolov5 trains its own data set to report error records
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.main参数指定可视化图像标题文本字体的大小
Servlet learning (II)
Servlet specification (I)
JDBC notes
Nebula Graph入驻阿里云计算巢,助力企业打造云上超大规模图数据库





![[HCTF 2018]WarmUp](/img/b0/6baee8ac76b56378230c2218f15734.png)



