积木大赛(Building block competition)

题目描述

春春幼儿园举办了一年一度的「积木大赛」。今年比赛的内容是搭建一座宽度为 n的大厦,大厦可以看成由 n 块宽度为 1 的积木组成,第 i块积木的最终高度需要是 hi。

在搭建开始之前,没有任何积木(可以看成 n 块高度为 0的积木)。接下来每次操作,小朋友们可以选择一段连续区间 [L, R],然后将第 L 块到第 R 块之间(含第 L 块和第 R 块)所有积木的高度分别增加 1。

小 M 是个聪明的小朋友,她很快想出了建造大厦的最佳策略,使得建造所需的操作次数最少。但她不是一个勤于动手的孩子,所以想请你帮忙实现这个策略,并求出最少的操作次数。

输入格式

输入包含两行,第一行包含一个整数 n,表示大厦的宽度。

第二行包含 n个整数,第 i 个整数为hi。

输出格式

仅一行,即建造所需的最少操作数。

样例

输入数据 1

5

2 3 4 1 2

输出数据 1

5

其中一种可行的最佳方案是依次选择 [1, 5], [1, 3], [2, 3], [3, 3], [5, 5]。

数据范围与提示

对于 30% 的数据,有 1≤n≤10;

对于 60% 的数据,有 01≤n≤1000;

对于 100% 的数据,有1≤n≤100000,0≤hi≤10000。

“`

//// Created by 23011 on 15/1/2022.//#include<iostream>using namespace std;const int maxn=100000+10;int a[maxn];int main(){    int n;    scanf("%d",&n);    for (int i=0;i<n;i++) scanf("%d",&a[i]);    int now=a[0],ans=a[0];    for(int i=1;i<n;i++){        if (a[i]>now) ans+=(a[i]-now);        now=a[i];    }    printf("%d",ans);    return 0;}
————————

< strong > Title Description < / strong >

Chunchun kindergarten held the annual “building block competition”. The content of this year’s competition is to build a building with a width of ^ n. The building can be regarded as composed of ^ n ^ blocks with a width of ^ 1 ^ and the final height of the ^ I block needs to be ^ hi.

Before the start of construction, there are no building blocks (which can be regarded as # n # blocks with a height of # 0). Next, for each operation, children can select a continuous interval [l, R], and then increase the height of all building blocks from ^ L ^ block to ^ R ^ block (including ^ L ^ block and ^ R ^ block) by ^ 1 respectively.

Little m is a smart child. She quickly came up with the best strategy for building a building to minimize the number of operations required. But she is not a diligent child, so she wants to ask you to help implement this strategy and find the minimum number of operations.

< strong > input format < / strong >

The input contains two lines. The first line contains an integer n, which represents the width of the building.

The second line contains , n integers, and the , I , integer is hi.

< strong > output format < / strong >

Only one row, the minimum number of operands required for construction.

样例

< strong > input data1 < / strong >

five

2 3 4 1 2

< strong > output data1 < / strong >

five

One of the feasible best schemes is to select [1,5], [1,3], [2,3], [3,3], [5,5] successively.

Data range and tips

For 30% of the data, there is ﹤ 1 ≤ n ≤ 10;

For 60% of the data, there are 0.01 ≤ n ≤ 1000;

For 100% data, there are 1 ≤ n ≤ 100000, 0 ≤ hi ≤ 10000.

“`

//// Created by 23011 on 15/1/2022.//#include<iostream>using namespace std;const int maxn=100000+10;int a[maxn];int main(){    int n;    scanf("%d",&n);    for (int i=0;i<n;i++) scanf("%d",&a[i]);    int now=a[0],ans=a[0];    for(int i=1;i<n;i++){        if (a[i]>now) ans+=(a[i]-now);        now=a[i];    }    printf("%d",ans);    return 0;}