Problem 1567-D-Sloth & #39; s Angry (recursion + greedy)
Problem 1567-D-Sloth's Angry
Time Limit: 1000 MS Memory Limit: 65536KB
Total Submit: 326 Accepted: 113 Special Judge: No
Description
A forest is full of sloths, they are so eager for tree leaves and as a result, very angry.
We assume that the forest is a map of N * M grids, and each of the gird is an empty land or contains a big sloth. it's guaranteed that the sequence of the sloth is a continuous segment from the leftmost column for every row. (You may consider that the number of columns M is infinite ).
As a sloth lover, you want to feed all the sloths as quick as possible. Every second you may select a rectangle area of the grids which only contains sloths and feed all the sloths there.
What's the minimum time required to meet all the sloths 'needs in the forest?
Input
First line of each case contains one numbers N. (1 ≤ ?? N? ≤? 1? 000 ).
The following line contains N numbers Ai, each describing the length of continuous sloths sequence from the left most column in every row. (1 <= Ai <= 10 ^ 9)
Output
Output the answer on a single line for each case.
Sample Input
4
3 4 5 5
Sample Output
3
Hint
The distributing situation of the sloths in the sample is as follow:
SSS
SSSS
SSSSS
SSSSS
And you can choose three rectangles to cover it.
Source
This question is a bit of a discrete thought. For example, it takes two times for 3 4 and two times for 100,101, so you don't have to worry about how to change the original value after each operation, keep the original value unchanged,
For example, if you want to subtract 2 from each of the three 4 2 5 values, the value is 1 2 0 3, but the fact is the same as 3 4 0 5. Further, the value is 3 4 [2] 5, the recursive algorithm is easy to write because it does not need to be changed in connection with 2.
Solve (L, R) indicates the number of times the range [L, R] is overwritten by a rectangle.
# Include
Using namespace std; template
Inline T read (T & x) {char c; while (c = getchar () <= 32) if (c = EOF) return 0; bool OK = false; if (c = '-') OK = true, c = getchar (); for (x = 0; c> 32; c = getchar ()) x = x * 10 + c-'0'; if (OK) x =-x; return 1;} template
Inline T read _ (T & x, T & y) {return read (x) & read (y);} template
Inline T read _ (T & x, T & y, T & z) {return read (x) & read (y) & read (z);} template
Inline void write (T x) {if (x <0) putchar ('-'), x =-x; if (x <10) putchar (x + '0'); else write (x/10), putchar (x % 10 + '0');} template
Inline void writeln (T x) {write (x); putchar ('\ n');} // ------- zcc io template ------ const int maxn = 1e6 + 10; const double inf = 999999999; # define lson (rt <1), L, M # define rson (rt <1 | 1), M + 1, R # define M (L + R)> 1) # define For (I, t, n) for (int I = (t); I <(n ); I ++) typedef long LL; typedef double DB; typedef pair
P; # define bug printf ("--- \ n"); # define mod merge into LL a [maxn]; int n; LL solve (int l, int r) {LL len = LL (r-l + 1), sum = 1, pos = l; LL minv = a [l]; for (int I = l; I <= r; I ++) minv = min (minv, a [I]); // greedy lies in finding the minimum value on the right each time, for (int I = l; I <= r; I ++) {if (a [I] = minv) {sum + = solve (pos, i-1); // change the position of I to 0, calculate the number of pos = I + 1; // update the position of pos} if (pos <= r) // sum + = solve (pos, r); return min (len, sum); // minimum value} int main () {// freopen ("in.txt", "r", stdin); while (read (n) {for (int I = 1; I <= n; I ++) read (a [I]); writeln (solve (1, n);} return 0;}/* 43 4 5 543 4 2 563 4 1 3 4 1 */