当前位置:网站首页>Air conditioning (daily question 50 in spring)
Air conditioning (daily question 50 in spring)
2022-06-22 05:48:00 【sweetheart7-7】
Farmer John Of N N N Cows are very picky about the room temperature in their barn .
Some cows prefer a lower temperature , Some cows prefer higher temperatures .
Farmer John The cowshed contains a row of N N N Cattle pen , The number is 1 … N 1…N 1…N, There is a cow in each corral .
The first i i i The cow wants the temperature in her barn to be p i p_i pi, And now the temperature in her bullpen is t i t_i ti.
To make sure every cow feels comfortable ,Farmer John A new air conditioning system has been installed .
The way the system is controlled is very interesting , He can send commands to the system , Tell it to raise or lower the temperature in a continuous set of corrals 1 1 1 A unit of —— for example 「 Corral the cattle 5 … 8 5…8 5…8 The temperature of 1 1 1 A unit of 」.
A continuous set of bullpen can contain only one bullpen .
Please help Farmer John Find the minimum number of commands he needs to send to the new air conditioning system , Make each cow's barn at the ideal temperature of the cow in it .
Input format
The first line of input contains N N N.
The next line contains N N N Nonnegative integers p 1 … p N p_1…p_N p1…pN, Separate... With spaces .
The last line contains N N N Nonnegative integers t 1 … t N t_1…t_N t1…tN.
Output format
Output an integer , by Farmer John The minimum number of instructions to use .
Data range
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
sample input :
5
1 5 3 3 4
1 2 2 2 1
sample output :
5
Sample explanation
A set of optimal Farmer John The instructions you can use are as follows :
initial temperature :1 2 2 2 1
Raise the cowshed 2..5:1 3 3 3 2
Raise the cowshed 2..5:1 4 4 4 3
Raise the cowshed 2..5:1 5 5 5 4
Lower the cowshed 3..4:1 5 4 4 4
Lower the cowshed 3..4:1 5 3 3 4
Ideas :
It is equivalent to changing the second array into the first array ( Only one continuous interval can be tested at a time + 1 +1 +1 perhaps − 1 -1 −1 operation )
You can subtract the corresponding position number of the first array from the second array ( That is, their difference )
The corresponding meaning of the title is to change all the elements of the difference array into 0 0 0
Then find the difference array of the difference array ( The original array is all changed to 0 0 0 The corresponding difference becomes 0 0 0)
An interval operation on the original array is equivalent to changing two array elements of the difference group ( If the operation is [ l , r ] [l, r] [l,r] Section +1, It's equivalent to Bad branch [ l ] + 1 , Bad branch [ r + 1 ] − 1 Difference [l] + 1, Difference [r + 1] - 1 Bad branch [l]+1, Bad branch [r+1]−1 ), The difference array becomes 0 0 0, You need to do m a x max max( Sum of positive numbers , Sum of absolute values of negative numbers ) operations .
If s u m a > s u m b sum_a > sum_b suma>sumb , Before s u m b sum_b sumb The second operation is to subtract a positive number 1 1 1, Add... To a negative number 1 1 1, The rest s u m a − s u m b sum_a - sum_b suma−sumb The second operation is performed on the remaining positive or negative numbers − 1 / + 1 -1/+1 −1/+1 operation , And yes n + 1 n+1 n+1 Position to proceed + 1 / − 1 +1/-1 +1/−1 operation ( n + 1 ) (n+1) (n+1) The element of position has no effect on the original array .
#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;
}
边栏推荐
- Learning method 3 for promotion of large enterprises: chain learning method
- nacos server 源码运行实现
- 关于图片懒加载的实现(总结梳理)
- 独立站优化清单丨如何有效提升站内转化率?
- Analysis of 43 cases of MATLAB neural network: Chapter 29 research on the application of limit learning machine in regression fitting and classification -- Comparative Experiment
- 基于CRU中的tmp数据进行年平均气温分析
- open source hypervisor
- RGB, sRGB and XYZ coordinate conversion
- CMAKE notes
- Sourcetree reported an error SSH failure
猜你喜欢
随机推荐
CMAKE notes
From "platform transformation" to "DTC brand going to sea", what is the trend of 2021?
vscode 远程连接错误:Server status check failed - waiting and retrying
Analysis on the development status of China's copper aluminum composite bus industry and Research Report on investment opportunities 2022-2027
Learning method 4 for promotion of big factories: play learning method
电脑卡顿怎么办?
Analysis of 43 cases of MATLAB neural network: Chapter 29 research on the application of limit learning machine in regression fitting and classification -- Comparative Experiment
The first week of wechat applet development: page setup, page Jump and data binding
Implementation of lazy loading of pictures (summary and sorting)
Machine learning note 7: powerful neural network representation
Facing the bonus period of Google traffic, how do independent station sellers take advantage of the situation to market?
Hide symbol of dynamic library
count registers in C code -- registers has one pattern
P1061 [noip2006 popularization group] counting method of jam
[graduation season · advanced technology Er] a graduate student's chatter
Zhiyuan OA vulnerability analysis, utilization and protection collection
u盘作为启动盘重装win10系统(无需其他软件)
數據的存儲(進階)
Adaboost
Implementation of Nacos server source code








