Noip2013 Day2 T1 Brick competition (Simple linear DP)

Source: Internet
Author: User
Title 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 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/Output sample Input Sample # #:

5
2 3 4 1 2 Output Example # #:

5 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.




Really is a super simple moving title (said simulation is not too) code (not to write the idea because it is written in the code)

#include <cstdio>
using namespace std;
int n,h[100005],f[100005];
H record the height to reach, F record the number of times required for the current position 
inline int read ()
{
    char ch=getchar ();
    int sum=0;
    while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar ();
    while (ch<= ' 9 ' &&ch>= ' 0 ') Sum=sum*10+ch-48,ch=getchar ();
    return sum;
}
void Input ()
{
    n=read ();
    for (int i=1;i<=n;i++) h[i]=read ();
}
void Dp ()
{
    f[1]=h[1];
    for (int i=2;i<=n;i++)
     {
        if (h[i]>h[i-1]) f[i]=f[i-1]+h[i]-h[i-1];
        If the current height is greater than the previous height, it will need to be stamped h[i]-h[i-1] times 
        else f[i]=f[i-1];
        If the current height is less than or equal to the previous height, then the previous cover can be covered together 
     }
}
int main ()
{
    Input ();
    Dp ();
    printf ("%d\n", F[n]);
    return 0;
}

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.