Longest ascending sub-sequence of HDU 1950

Source: Internet
Author: User
1 // accepted 3540 kb 62 MS 2 // DP longest ascending subsequence 3 # include <cstdio> 4 # include <cstring> 5 # include <iostream> 6 using namespace STD; 7 const int imax_n = 400005; 8 int DP [imax_n]; 9 int d [imax_n]; 10 int A [imax_n]; 11 int N; 12 INT Len; 13 int max (int A, int B) 14 {15 return A> B? A: B; 16} 17 int binary_search (int K) 18 {19 int L = 0; 20 int r = Len; 21 While (L <= r) 22 {23 int mid = (L + r)/2; 24 if (d [Mid] = k) return mid; 25 if (d [Mid] <K) L = Mid + 1; 26 if (d [Mid]> K) r = mid-1; 27} 28 return l; 29} 30 void dp () 31 {32 memset (DP, 0, sizeof (DP); 33 memset (D, 0, sizeof (d); 34 Len =-1; 35 For (INT I = 0; I <n; I ++) 36 {37 Int J = binary_search (A [I]); 38 If (j> Len) len ++; 39 DP [I] = J + 1; 40 d [J] = A [I]; 41} 42 int ans = 0; 43 for (INT I = 0; I <n; I ++) 44 ans = max (ANS, DP [I]); 45 printf ("% d \ n ", ans); 46} 47 int main () 48 {49 int t; 50 scanf ("% d", & T); 51 while (t --) 52 {53 scanf ("% d", & N); 54 for (INT I = 0; I <n; I ++) 55 scanf ("% d ", & A [I]); 56 dp (); 57} 58 return 0; 59}
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.