Codeforces 229/D Dynamic Planning

Source: Internet
Author: User

Codeforces 229/D Dynamic Planning

 

D. Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

The city of D consistsNTowers, built consecutively on a straight line. The height of the tower that goesI-Th (from left to right) in the sequence equalsHI. The city mayor decided to rebuild the city to make it beautiful. In a beautiful city all towers are arranged in non-descending order of their height from left to right.

The rebuilding consists of parameter Ming several (perhaps zero) operations. an operation constitutes using a crane to take any tower and put it altogether on the top of some other neighboring tower. in other words, we can take the tower that standsI-Th and put it on the top of either (IRows-Second 1)-th tower (if it exists), or (ISecond + second 1)-th tower (of it exists ). the height of the resulting tower equals the sum of heights of the two towers that were put together. after that the two towers can't be split by any means, but more similar operations can be saved med on the resulting tower. note that after each operation the total number of towers on the straight line decreases by 1.

Help the mayor determine the minimum number of operations required to make the city beautiful.

Input

The first line contains a single integerN(1 digit ≤ DigitNLimit ≤ limit 5000)-the number of towers in the city. The next line containsNSpace-separated integers:I-Th numberHI(1 digit ≤ DigitHILimit ≤ limit 105) determines the height of the tower that isI-Th (from left to right) in the initial tower sequence.

Output

Print a single integer-the minimum number of operations needed to make the city beautiful.

Sample test (s) input
58 2 7 3 1
Output
3
Input
35 2 1
Output
2

Question:

 

There are n towers in this city. For the sake of appearance, the tower behind it cannot be shorter than the one in front of it, so you need to organize it, you can place any tower on another tower to form a new one,

Note that you can only move it to the adjacent tower. The new tower can continue to move and ask you how many steps can be completed.

Ideas:

At first, I thought it was greedy and wanted to solve the problem of O (n) complexity. The Code was also written. After a lot of data, CF gave it to the background, when I looked at my WA data, I realized that my thinking was totally wrong and I threw it away, it took several months to figure out a DP Solution:

Two-dimensional DP, dp [I] [j]: I represents step j of tower I's movement, which is the shortest possible. dp is constantly updated, run O (n ^ 2) once to solve the problem,

 

# Include
 
  
Using namespace std; int dp [5002] [5002]; // the I-th tower. The height of step j is used to press forward and backward const int inf = 0x7f7f7f7f; class Solution {int n, a [5002]; public: Solution (int _ n): n (_ n) {for (int I = 0; I
  
   

 

 

 

 

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.