NOIP 2018 to improve the group questions resolved Day1 T1 paved roads P5019

Title Description

Chris is a road engineer, responsible for the laying of a length of  the n- the n-road.

The main paved road is filled subsidence of the surface. The entire section of the road can be regarded as  n region is connected to the n-block sum of a start, the first  I I block area depressed depth  D_i D I  .

May be selected for a continuous interval Chris day [L, R & lt] [ L , R & lt ], each filled region of this interval, the allowed depth to reduce sag  . 1 1 When selection interval, required to ensure that, within each region sag depth interval before the filling is not  0 0

Chris Hope you can help him design a solution that can in the shortest possible time the whole depth of the sunken roads have become  0 0

Input Format

Input file contains two lines, the first line contains an integer  n- n-, represents the length of the road. The second line contains  n- n-integers, separated by a space between adjacent two numbers, the first I I is the integer  D_i D I  .

Output Format

The output file contains only one integer, that requires a minimum number of days to complete the task.

Sample input and output

Input # 1
6   
4 3 2 5 3 5 
Output # 1
9

Description / Tips

Sample [explain]

One possible preferred embodiment is select:  [1,6] [ 1 , 6 ], [1,6] [ 1 , 6 ], [1,2] [ 1 , 2 ], [1,1] [ 1 , 1 ], [4,6] [ 4 , 6 ], [4,4] [ 4 , 4 ], [4,4] [ 4 , 4 ], [6,6] [ 6 , 6 ], [6,6] [ 6 , 6 ].

[Agreed] with the scale data

For  30 \% . 3 0 % of the data, . 1 ≤ n-≤ 10 . 1 n- . 1 0;
for  70 \% . 7 0 % of the data, . 1 ≤ n-≤ 1000 . 1 n- . 1 0 0 0;
for  100 \% . 1 0 0 % of the data, . 1 ≤ ≤ n-100000, 10000 ≤ 0 ≤ D_i . 1 n- . 1 0 0 0 0 0 , 0 D I . 1 0 0 0 0.

 
 
The questions I saw after the first feeling is as if it should be very simple and seemed to be very difficult
According to my impression that I had done a similar title
I remember it was like this
And then the original number as 432 535 between the two numbers the number of columns in each adjacent adds up to is that 1 + 1 + 2 + 3 + 2 = 9 things really there is a coincidence it? I have incredible eh
 
Let us give an example to try 
1234561 + 1 + 1 + 1 + 1 = 5 vicissitudes over the death of my son should be 6 times
234 711 + 1 + 3 + 6 = 11 ah nor should be 7
 
Stop, stop must be the wrong place
Always feel poor and count is a positive solution
 
Well we still have eight children to being the first to prove it to look at it
 
Example, there are two pits of pits immediately preceding pit after
Pit depth of 5 pit depth is 2 then cut together affirmative decision this time as long as 1 minus 5 5 3 2 will follow disappear together
That! ! ! ! !
We enumerate the front to back if a [i]> a [i-1] then set a sum accumulator sum + = a [i] -a [i-1]
Then a [1] -a [0] does not prevent a [0] is just the thing 0
 
 
So if we carry on like this deal with the case
It is equivalent to using a greedy algorithm
 
We calculate the equivalent of each pit will bring much more than answer contributions
So how to verify the correctness of the child greedy algorithm it?
Very simple to find a few examples can be found into the math is right up     
We take a sample of this question come to understand about it
4 3 2 5 3 5
(Cat represents how you have not finished!
A start value sum is zero
First we look at the 4 sum is sure to add a 4 - 0 That's certainly at least be able to go through four days of the depth of the pit 4 to fill out the sum = 4
Let's look at 33 do not operate more than four hours that is in the process of covering the 4 finished in 3 along with it "free" to fill out the
2 is the same situation as 3 <4 (cat means that you this is not bullshit you! That is three times while filling out 2 is also filled
There was not the same situation then look 5 5> 2 so the sum to add 3 sum = 7 2 that is filled out when the original depth of the pit there is no depth of 5 3 So fill out again tired plus 3 and 4 at this time why the relationship is not it? It is clear already become 2 0 4 and 5 this time it separates the
Look 3 obviously do not control
Look at that last 5 3 that is filled out when there is a depth of 5 2 does not fill again plus 2 sum = sum between two 5-9 is not associated with depth because when they have become 2 when 0 3 that place has become that they are spaced apart can not be reduced to 2 were considered together
 
 
Li again feel so much more fun so the algorithm can manage it down is the right of it
 
 
very happy!
Expect Score: 100 Score: 100
code show as below:
That unsigned long long feel no great need to take precaution Well
#include<bits/stdc++.h>
#define maxn 100005
using namespace std;
int arr[maxn]; 
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
    unsigned long long ans=0;
    for(int i=1;i<=n;i++) 
        //if(arr[i]>arr[i-1])
        ans+=1ull*(arr[i]>arr[i-1])*(arr[i]-arr[i-1]);
    printf("%llu",ans);
    return 0;
} 

 

 
 
 
 
 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11412163.html