Nyoj 214 monotonic incrementing subsequence (2)

Source: Internet
Author: User
Monotonic incrementing subsequence (2)Time Limit: 1000 MS | memory limit: 65535 kb difficulty: 4
Description

Given an integer series {A1, a2..., an} (0 <n <= 100000), locate the monotonic incrementing oldest sequence and obtain its length.

For example, the longest monotonic increasing subsequence of 1 9 10 5 11 2 13 is 1 9 10 11 13 with a length of 5.

 
Input
Multiple groups of test data (<= 7)
The first row of each group of test data is an integer N, indicating a total of N integers in the sequence, followed by N integers in the next row, indicating all elements in the series. separate each integer with spaces (0 <n <= 100000 ).
Data ends with EOF.
The input data must be valid (all are int integers )!
Output
For each group of test data, the length of the output integer sequence is the longest incrementing sub-sequence. Each output occupies one row.
Sample Input
71 9 10 5 11 2 1322 -1
Sample output
51

Solution: The data volume is large, and the O (N ^ 2) method does not work! Only the O (nlogn) algorithm is available.

Attach the Tle code first.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 int d[100010],dp[100010];12 int main(){13     int n,i,j,ans;14     while(~scanf("%d",&n)){15         for(i = 1; i <= n; i++){16             scanf("%d",d+i);17             dp[i] = 1;18         }19         ans = 1;20         for(i = 2; i <= n; i++){21             for(j = i-1; j; j--){22                 if(d[j] < d[i] && dp[i] < dp[j]+1) dp[i] = dp[j]+1;23             }24             if(dp[i] > ans) ans = dp[i];25         }26         printf("%d\n",ans);27     }28     return 0;29 }
View code

 

Okay, the AC code and the O (nlogn) algorithm are exquisite in half-lookup and fast. Oh, the last top? Top: 1 can be removed. It's funny! The loss is 4 ms.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 int stk[100010],top;12 void bsearch(int lt,int rt,int val) {13     int mid;14     while(lt <= rt) {15         mid = (lt+rt)>>1;16         if(val < stk[mid]) rt = mid-1;17         else lt = mid+1;18     }19     stk[lt] = val;20 }21 int main() {22     int n,i,temp;23     while(~scanf("%d",&n)) {24         top = 0;25         for(i = 1; i <= n; i++) {26             scanf("%d",&temp);27             if(!top || stk[top-1] < temp)28                 stk[top++] = temp;29             else bsearch(0,top-1,temp);30         }31         printf("%d\n",top?top:1);32     }33     return 0;34 }
View code

 

 

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.