3288 Bricks Competition

Source: Internet
Author: User

3288 Bricks Competition

2013 Noip National League Improvement Group

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingTitle 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.

Enter a description Input Description

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 description Output Description

Only one row, which is the minimum number of operations required to build.

Sample input Sample Input

2 3 4) 1 2

Sample output Sample Output


Data range and Tips Data Size & Hint

One of the best possible scenarios, select [1,5] [1,3] [2,3] [3,3] [5,5]
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.


#include <iostream>#include<cstdio>using namespacestd;intnums[100100],ans,n;intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d", nums+i);  for(intI=1; i<=n;i++) ans+=max (nums[i]-nums[i-1],0); printf ("%d\n", ans); return 0;}


===== No. 0 Column ↑==== 1th column ↑==== 2nd column ↑==== 3rd column ↑==== 4th column ↑==== 5th column ↑

Above is a sample ↑ from 0 ~n;

The code means: the comparison plus the positive difference between column I and the previous column if the minus is negative then + 0;

–> Proof (1)

If the minus is a positive number, then if you raise this column higher than the previous column then you need to ' poor ' (the 1th column and the previous column's ' Difference ' is 2) to raise this column to the specified height


If the minus is a positive number, then it is possible to take this line up with the other raised bricks before I column is 0; (the 2nd column needs to be raised by 1 blocks compared to the first column because the 3rd grid and the following are already ascending with the 1th column)

Proof is complete.

Although short but not very good to think ah = = Orz

3288 Bricks Competition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.