Title Description Description
Spring and Spring Kindergarten held the annual "Building Block Contest". This year's game is to build a building with a width of n, the building can be seen by the n block width of 1 blocks, the final height of block I need to be hi.
Before the construction begins, there is no building block (it can be seen as a block of n blocks of height 0). After each operation, the children can select a continuous interval [L, r] and then increase the height of all blocks from block L to block R (including Block L and R Block) by 1 respectively.
Little M was a clever little friend, and she soon came up with the best strategy for building, making the minimum number of operations required. But she is not a diligent child, so I would like to ask you to help achieve this strategy, and to find the minimum number of operations.
Input/output format Input/output
Input format:
Input file is block.in
The input contains two lines, and the first row contains an integer n, which indicates the width of the building.
The second line contains n integers, and the first integer is hi.
Output format:
Output file is Block.out
Only one row, which is the minimum number of operations required to build.
Input and Output Sample sample Input/output
Sample test point # # Input Example:
5
2 3 4) 1 2
Sample output:
5
Description description
"Sample Interpretation"
One of the best possible scenarios, select
[1,5] [1,3] [2,3] [3,3] [5,5]
"Data Range"
For 30% of the data, there are 1≤n≤10;
For 70% of the data, there are 1≤n≤1000;
For 100% of the data, there is 1≤n≤100000,0≤hi≤10000.
Topic Analysis:
The subject surface is a simulation, in fact, is a simulation ... But this problem online many Daniel have adopted the NLOGN algorithm ... is the online data too weak? The O (n) code solves this problem and has been evaluated by TYVJ and Rokua. (In the beginning, I forgot to add Cstdio library, wasted two times, my pass rate ah ... )
#include <iostream>#include<cstdio>using namespacestd;intMain () {intn,now=0, ans=0; scanf ("%d",&N); for(intI=1; i<=n; i++) { intT; scanf ("%d",&t); if(T>now) ans+=t-Now ; now=T;} cout<<ans;return 0;}
Back Gear | Block Contest