Dynamic Planning of garlic clientworker and garlic clientworker

Source: Internet
Author: User

Dynamic Planning of garlic clientworker and garlic clientworker

Question:

There are n wooden piles in front of garlic, and the height of the piles is h1, h2, h3 running hn. In the first step, you can jump to any Wooden Pile. In the next step, you can only jump back, and the height of the next Wooden Pile is not higher than that of the current Wooden Pile. I hope you can step on as many wooden stakes as possible. Please help calculate the maximum number of wooden stakes you can step on.
Input Format

Enter an integer n in the first line to indicate the number of wooden piles. In the second row, enter n integers h1, h2, h3 hybrid hn, representing the height of n wooden piles. (1 ≤ n ≤ 100000, 1 ≤ hi ≤)


Output Format

The output is an integer that represents the maximum number of piles that can be stepped on, occupying one row.


Sample Input

6
3 6 4 1 4 2

Sample output

4

 

Ideas

 

It is not hard to find that, at this moment, a question about LIS (the longest ascending subsequence. (Of course, this is the longest sub-sequence that does not rise .)

 

The O (n2) Complexity Algorithm cannot satisfy all the data, so the O (nlogn) algorithm is used.

 

The idea of the O (nlogn) algorithm is to store a minimum value corresponding to the dp value, which must be in ascending order. For example, the program stores it in the ans array, so that the next time a new minimum number appears, we can use binary to find the fastest place. In the program, the variable pos is placed in the position of a [I], and the variable len is the longest length, that is, the answer. Mark
 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cstring> 6 using namespace std; 7 int n,a[1000+5],dp[1000+5],ans[1000+5],len; 8 int binarySearch(int arr[], int first, int end, int tar) 9 {10     while (end > first)11     {12         int mid = (first+end)/2;13         if (arr[mid]<tar)14         {15             end = mid;16         }17         else18         {19             first = mid+1;20         }21     }22     return end;23 }24 int main()25 {26     cin >> n;27     for (int i=1; i<=n; i++)28     {29         cin >> a[i];30     }31     ans[1] = a[1];32     len = 1;33     for (int i=2; i<=n; i++)34         if (a[i] <= ans[len])35             ans[++len] = a[i];36         else37         {38             int pos = binarySearch(ans,1,len,a[i]);39             ans[pos] = a[i];40         }41     cout << len << endl;42     return 0;43 }

 

 

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.