Luo Gu P5019 paved roads & [NOIP2018 improve group] (greedy)

Topic Link

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

Problem-solving ideas

A typical greedy title.

Assuming that fill the hole left to right, if the i-th deep and i + 1-th, then the i + 1-th no additional fill;

If the i + 1-th greater than the i-th, it is necessary to fill the depth of the i + 1-i, so the array is equivalent to the difference> 0 add up the AC.

AC Code

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

// NOIP2018 improve group Day1 t1

Guess you like

Origin www.cnblogs.com/yinyuqin/p/11266394.html