当前位置:网站首页>空调(春季每日一题 50)
空调(春季每日一题 50)
2022-06-22 05:31:00 【sweetheart7-7】
Farmer John 的 N N N 头奶牛对他们牛棚的室温非常挑剔。
有些奶牛喜欢温度低一些,而有些奶牛则喜欢温度高一些。
Farmer John 的牛棚包含一排 N N N 个牛栏,编号为 1 … N 1…N 1…N,每个牛栏里有一头牛。
第 i i i 头奶牛希望她的牛栏中的温度是 p i p_i pi,而现在她的牛栏中的温度是 t i t_i ti。
为了确保每头奶牛都感到舒适,Farmer John 安装了一个新的空调系统。
该系统进行控制的方式非常有趣,他可以向系统发送命令,告诉它将一组连续的牛栏内的温度升高或降低 1 1 1 个单位——例如「将牛栏 5 … 8 5…8 5…8 的温度升高 1 1 1 个单位」。
一组连续的牛栏最短可以仅包含一个牛栏。
请帮助 Farmer John 求出他需要向新的空调系统发送的命令的最小数量,使得每头奶牛的牛栏都处于其中的奶牛的理想温度。
输入格式
输入的第一行包含 N N N。
下一行包含 N N N 个非负整数 p 1 … p N p_1…p_N p1…pN,用空格分隔。
最后一行包含 N N N 个非负整数 t 1 … t N t_1…t_N t1…tN。
输出格式
输出一个整数,为 Farmer John 需要使用的最小指令数量。
数据范围
1 ≤ N ≤ 1 0 5 1≤N≤10^5 1≤N≤105,
0 ≤ p i , t i ≤ 10000 0≤p_i,t_i≤10000 0≤pi,ti≤10000
输入样例:
5
1 5 3 3 4
1 2 2 2 1
输出样例:
5
样例解释
一组最优的 Farmer John 可以使用的指令如下:
初始温度 :1 2 2 2 1
升高牛棚 2..5:1 3 3 3 2
升高牛棚 2..5:1 4 4 4 3
升高牛棚 2..5:1 5 5 5 4
降低牛棚 3..4:1 5 4 4 4
降低牛棚 3..4:1 5 3 3 4
思路:
相当于将第二个数组变为第一个数组(每次只能对连续的一段区间进行 + 1 +1 +1 或者 − 1 -1 −1 操作)
可以先用第二个数组减去第一个数组对应位置数字(即它们的差)
题目对应的意思就是把这个差数组所有元素变为 0 0 0
然后求这个差数组的差分数组(原数组全部变为 0 0 0 也就对应的差分变为 0 0 0)
对原数组进行一段区间的操作相当于对差分数组变两个数组元素(假如操作的是 [ l , r ] [l, r] [l,r]区间+1,则相当于 差 分 [ l ] + 1 , 差 分 [ r + 1 ] − 1 差分[l] + 1, 差分[r + 1] - 1 差分[l]+1,差分[r+1]−1 ),差分数组变为 0 0 0,则需要进行 m a x max max(正数之和,负数绝对值之和)次操作。
假如 s u m a > s u m b sum_a > sum_b suma>sumb ,则前 s u m b sum_b sumb次操作为将某个正数减 1 1 1,将某个负数加 1 1 1,剩下的 s u m a − s u m b sum_a - sum_b suma−sumb 次操作为对剩下的正数或负数进行 − 1 / + 1 -1/+1 −1/+1 操作,而对 n + 1 n+1 n+1 位置进行 + 1 / − 1 +1/-1 +1/−1 操作 ( n + 1 ) (n+1) (n+1) 位置的元素对原数组没有影响。
#include<iostream>
using namespace std;
const int N = 100010;
int n;
int a[N];
int main(){
cin >> n;
for(int i = 0; i < n; i++ ) scanf("%d", &a[i]);
int x;
for(int i = 0; i < n; i++) scanf("%d", &x), a[i] -= x;
for(int i = n - 1; i > 0; i--) a[i] -= a[i-1];
int neg = 0, pos = 0;
for(int i = 0; i < n; i++)
if(a[i] > 0) pos += a[i];
else neg -= a[i];
printf("%d\n", max(neg, pos));
return 0;
}
边栏推荐
- c files always get rebuild when make -------- . PHONY in Makefile
- redis连接错误:ERR Client sent AUTH, but no password is set解决方案2个
- 通达OA漏洞分析合集
- 删除弹窗组件的封装使用
- Link a static library‘s all sections
- Development prospect forecast and investment strategy analysis report of global and Chinese manganese oxide nano powder industry 2022-2028
- nacos server 源码运行实现
- Overview analysis and investment forecast report of semiconductor refrigeration devices in the world and China 2022-2027
- Learning method 3 for promotion of large enterprises: chain learning method
- Small and medium-sized enterprises should pay attention to these points when signing ERP contracts
猜你喜欢

机器学习笔记 七:强大的神经网络表述

爬虫初始及项目

总有人问我:独立站该怎么玩?3个案例,你看完就懂了

The first week of wechat applet development: page setup, page Jump and data binding

Machine learning Note 6: number recognition of multiple classification problems in logistic regression

在线文本代码对比工具

nacos server 源码运行实现

Implementation of Nacos server source code

通达OA漏洞分析合集

做事方法:3C 方案设计法
随机推荐
\[\e]0;\[email protected]\h: \w\a\]\[\033[01;32m\]\[email protected]\h\[\033[0
Hide symbol of dynamic library
Implementation of large file fragment uploading based on webuploader
Optimization direction of code walk through (convenient interface requests, long dynamic class judgment conditions, removal of useless consoles, separation of public methods)
Which is the trend of cross-border policy frequent adjustment of "independent stations & platforms"?
2022 Shanxi secondary vocational group "Cyberspace Security" event module b- web page penetration
Tensorflow 2. Chapter 14: callbacks and custom callbacks in keras
nacos server 源码运行实现
Tensorflow 2.x(keras)源码详解之第十四章:keras中的回调及自定义回调
Remove then add string from variable of Makefile
Implementation of Nacos server source code
爬虫初始及项目
Performance analysis and test of interprocess communication methods under dual core real-time system
基于WebUploader实现大文件分片上传
Global and Chinese digital imager market development outlook and investment competitiveness analysis report 2022-2027
企业如何把ERP项目自动施行?
关于背包问题的总结
Remove then add string from variable of Makefile
count registers in C code -- registers has one pattern
亚马逊和独立站,不是简单的二选一