Shuoj1936-d sequence-Longest ascending subsequence

Source: Internet
Author: User

Description

It is known that two arrays A and B with a length of n are labeled from 0 to N-1.

Now define a sequence of D (assuming a length of L), which satisfies the following conditions:

1.0 <= d[i] <= N-1

2. A[d[i] < A[d[i+1] (0 <= i < L-1)

3. B[d[i] > B[d[i+1] (0 <= i < L-1)

The maximum length of the D sequence that satisfies the condition.

(The reason this sequence is called the D-sequence is: This problem is the D problem)

Input

Multiple sets of data, the first row of each group of data is an integer n. (1 <= N <= 100000)

The second row has N positive integers a[0]~a[n-1].

The third row has N positive integers b[0]~b[n-1].

guarantees that all input integers are within the range of int.

Output

For each set of data, output an integer that represents the maximum length of the D sequence L.

Sample Input31 1 23 2 131 2 33 2 141 3 2 43 1 2 4
Sample Output
233
Ideas:: An array of a, B array with a as the first keyword, b for the second keyword in ascending order, and then the B is inverted to find the longest ascending subsequence of B.In order to avoid subscript sorting and write comparison functions, a B is saved in a pair and then taken out to hold the large to a. Invert, and seek the oldest sequence.When the longest ascending sub-sequence is obtained, the time complexity of the method of direct DP is O (N^2), which will time out, so the other method is used.method (1):: Using Lower_bound to find ascending sub-sequence O (NLOGN)Lower_bound three parameters are the start address to compare, the end point address +1 (that is, left closed right), the value to compare (assuming D).Its function is to return an address that is the address of the smallest value >=d within the range of comparisons.For example, a[] = {0, 1, 2, 4, 5, 7} p =lower_bound (a,a+6,3), p is the address of 4, if P =lower_bound (a,a+6,4), p is also 4 addressMethod (2):: Using binary method to find ascending subsequence O (nlogn)Using Lower_bound to compare in the array, when the number to compare is large, the number can not be stored in the array, and the use of dichotomy to solve the problem, but the code is difficult.The idea of both approaches is the same. The minimum value of the length of the array a neutron sequence i is stored in the array s. We take the example of 3 2 4 6 5 7 3 For a demonstration behavior traversal, listed as array s, where the change has been markedCome and help to understand. Here a[i] > s[J]&&a[i]<=s[j + 1] should place a[I] in the position of s[j+1]. So the key is to find out where J knows where to put a[I]. The above two methods are used to find the JOf (here Lower_bound directly back to J + 1) We can find that the values in the S array are necessarily sequential increments, which is a necessary condition for the use of the dichotomy method.
Demo
0 1 2 3 4
1 3
2 2
3 2 4
4 2 4 6
5 2 4 5
6 2 4 5 7
7 2 3 5 7

method (1) Code::
#include <bits/stdc++.h>using namespace Std;int a[100005],b[100005];int s[100005];vector<pair<int,int    > > t;//can be stored with vectors or directly with an array pair<int,int> t[100005];int main () {int n;        while (~SCANF ("%d", &n)) {t.clear ();//If the array is not initialized or to be faulted, the for (int i = 0;i<n;i++) scanf ("%d", &a[i]) is not required;        for (int i = 0;i<n;i++) scanf ("%d", &b[i]);        If the array should be t[i] = {A[i],b[i]};        for (int i = 0;i<n;i++) t.push_back (Make_pair (A[i],b[i]));        Sort (t,t+n); Std::sort (T.begin (), T.end ());//Sort for (int i= 0;i<n;i++) a[i] = t[i].second;//takes the sorted array B out and puts it in a reverse (a,a+n );//causes int len = 1; S[1] = A[0];//<span style= "font-family:arial, Helvetica, Sans-serif;"            > The first element is first put into s[1]</span> for (int i = 1;i<n;i++) {//DP, one by one adds the elements of a to S.            int t = a[i];            if (T>s[len]) S[++len] = A[i];                else{int p = lower_bound (s+1,s+len+1,t)-S;            S[p] = t;       } } printf ("%d\n", Len); } return 0;}
Method (2) Code::
#include <bits/stdc++.h>using namespace Std;int a[100005],b[100005];int s[100005];vector<pair<int,int > > T;    int main () {int n;        while (~SCANF ("%d", &n)) {t.clear ();        for (int i = 0;i<n;i++) scanf ("%d", &a[i]);        for (int i = 0;i<n;i++) scanf ("%d", &b[i]);        for (int i = 0;i<n;i++) t.push_back (Make_pair (A[i],b[i]));        Std::sort (T.begin (), T.end ());        for (int i= 0;i<n;i++) a[i] = T[i].second;        Reverse (a,a+n);        int len = 1;s[1] = a[0];            for (int i = 1;i<n;i++) {int t = A[i];            if (T>s[len]) S[++len] = A[i];                else{int L = 1,r = Len,mid;                int ans = 0;                    while (L&LT;=R)//The dichotomy here uses the idea of left closed right closed (Mid = (r+l)/2;                        if (s[mid]<t) {L = mid+1; Ans=max (ans, mid);//ans is the thought of J,j must be the largest number of s in the array less than t} else R = Mid-1;            } s[ans+1] = t;    }} printf ("%d\n", Len); } return 0;}

Copyright notice: You are free to browse.

Shuoj1936-d sequence-Longest ascending subsequence

Related Article

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.