Luo Gu P5019 paved roads (differential)

Ok...

 

Topic link: https: //www.luogu.org/problem/P5019

 

First, it simplifies the meaning of the questions:

Given an array of length N, each operation can select a range of minus 1, at least ask how many operations can be full array number becomes 0 N≤100000

 

Ideas:

First, for the first number we need to spend at least d_1 above d_i times, and then consider each d_i, that part of it for more than a numeric keypad (or equal to), and

We can operate at a time on a number of operations. If d_i> d_i - 1, which means it is larger than the previous number, then we must be more than d_i - d_i - 1 operations.

Obviously, there is a difference between number and number, and the difference will cause some problems interval, so the positive solution of this question is the difference ...

We maintain a difference array by b, then the difference is greater than 0 in the array can be added in ans.

 

AC Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 int ans, d[100005], b;
 7 
 8 int main(){
 9     int n;
10     scanf("%d", &n);
11     for(int i = 1; i <= n; i++){
12         scanf("%d", &d[i]);
13         b = d[i] - d[i - 1];
14         if(b > 0) ans += b;
15     }
16     printf("%d", ans);
17     return 0;
18 }
19     
AC Code

 

Guess you like

Origin www.cnblogs.com/New-ljx/p/11267087.html