# Luogu p2101 destiny stone door Selection

Source: Internet
Author: User

P2101 choice of destiny stone

Question description

Ganglun, an unknown world line, suddenly received a \ (DMail \) message today, saying that the world line would change dramatically, in the future, he will not be able to reverse this change to the world line. The reason for the change in the world line is that he soon missed his appointment with his assistant. He made an appointment with his assistant, but he had been in arrears with his rent before his appointment. The landlord asked him to help with the painting, however, he did not complete the task as quickly as possible, causing him to miss the appointment with his assistant, resulting in a drastic change in the world line. Now, to save the world, ganglun is not good at painting, so he finds you who are also not good at painting to help him solve this problem (this is the choice of destiny stone ). No matter how you save the world, although you are not good at painting, you can use programming to help you solve this problem.

This painting is very abstract: it consists of \ (n \) rectangles with a width of \ (1 \) and a height of \ (h_ I \). The rectangles are arranged side by side, there is no gap between adjacent rectangles. Initially, each rectangle has no color. You have a brush with a width of \ (1 \). You can use a vertical or horizontal brush. each time you use a brush, your brush must always be in a rectangle, that is, you cannot brush the image outside the rectangle. Of course, you cannot bend every time you brush the image. It takes \ (1 \) Time for you to refresh each time. This has nothing to do with the length of the refresh, for example, you can fl from the leftmost to the rightmost (without passing through the rectangle, of course), which only takes the \ (1 \) time. Your goal is to fill all the rectangles with colors. Please output the shortest time so that ganglun can decide whether to finish the task by yourself or let you do the hard work.

Input/output format:

Line \ (1 \): a positive integer \ (n \), indicating the number of rectangles.

The following \ (n \) positive integer \ (h_ I \) indicates the height of the \ (I \) rectangle.

Output Format:

An integer that represents the minimum time spent.

Input and Output sample input sample #1:
``52 2 1 2 1``
Output sample #1:
``3``
Description

[Data scale]

\ (30\% n \ Leq 20, h_ I \ Leq 100 \)

\ (60 \ % N \ Leq 100, h_ I \ Leq 1000 \)

\ (100 \%n \ Leq 5,000, h_ I \ Leq 10 ^ 9 \)

Ideas

This is a simple sub-rule or \ (DP. -- Logeadd

I can only do some sub-governance questions \ (qwq \).

We search for an interval, and then split it down one by one. Specifically, we use a function \ (DFS (L, R) \) to query the answer of the range \ ([L, R, and this answer can use multiple subintervals \ ([L, X_1], [x_1 + 1, x_2], [X_2 + 1, X_3] \ cdots [x_y + 1, r] \). In this way, we perform step-by-step search and merge the answer.

How can we merge the answers? There is an obvious conclusion for a range \ ([L, R] \): first fill the following blocks horizontally and then vertical the remaining intervals, so we can calculate the minimum height of this range, fill it horizontally, and then consider the remaining squares \ (DFS.

For example, we have a shape like this:

``    ■ ■  ■■   ■■■  ■■  ■■■■■■■■■■■■■■■■■■■■■■``

First, apply the following squares horizontally:

``    ■ ■  ■■   ■■■  ■■  ■■□□□□□□□□□□□□□□□□□□□□``

There are three more cells above, and then \ (DFS.

AC code
``#include<bits/stdc++.h>using namespace std;typedef long long LL;const LL MAXN=5e3+5;LL n,a[MAXN];LL read(){    LL re=0;char ch=getchar();    while(!isdigit(ch)) ch=getchar();    while(isdigit(ch)) re=(re<<3)+(re<<1)+ch-'0',ch=getchar();    return re;}LL ask(LL l,LL r){    if(l==r) return 1;    LL re=INT_MAX;    for(LL i=l;i<=r;i++) re=min(re,a[i]);    for(LL i=l;i<=r;i++) a[i]-=re;    for(LL i=l;i<=r;i++)    {        if(!a[i]) continue;        LL j=i;        while(j<=r&&a[j+1]) j++;        re+=ask(i,j),i=j;    }    return min(r-l+1,re);}int main(){    n=read();    for(LL i=1;i<=n;i++) a[i]=read();    printf("%lld",ask(1,n));    return 0;}``

Luogu p2101 destiny stone door Selection

Related Keywords:

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.