HDU 5324 Boring Class 2015 multi-school Joint Training Tournament 3 Division, the longest non-descending sub-sequence, the smallest dictionary order

Source: Internet
Author: User

Boring Class Time limit:6000/3000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 354 Accepted Submission (s): 75


Problem Description Mr Zstu and Mr Hdu is taking a boring class, Mr Zstu comes up with a problem to kill time, Mr. Hd U thinks it ' s too easy, he solved it very quickly, what's about you guys?
Here is the problem:
Give you sequences l1,l2,..., Ln and R1,r2,..., Rn.
Your task is to find a longest subsequence v1,v2,... vm satisfies
V1≥1, Vm≤n, vi<vi+1. (For I from 1 to m-1)
Lvi≥lvi+1, rvi≤rvi+1 (for I from 1 to m-1)
If there is many longest subsequence satisfy the condition, output the sequence which has the smallest lexicographic Orde R.



Input There is several test cases, each test case begins with an integer n.
1≤n≤50000
Both of the following, lines contain n integers describe the sequences.
1≤li,ri≤109

Output for each test case, output the A integer m indicates the length of the longest subsequence as described.
Output m integers in the next line.

Sample Input

5 5 4 3 2 1 6 7 8 9 10 2 1 2 3 4
Sample Output
5 1 2 3 4 5 1 1
Source multi-university Training Contest 3

To find the longest non-descending subsequence. Meet Li >= li+1 and Ri <= ri+1

The solving of the puzzle can be divided and tree set tree, I first write with set, found that the set can not complete the two-dimensional query. Then write with splay, timed out.

Tree sets of trees still do not know how to do the space, here will only n^2 the way of space.


This article uses the method of divide and conquer:

Since the minimum dictionary order is guaranteed, it is done from the backward and the size relation is reversed, and the longest ascending subsequence is obtained.

If the longest ascending subsequence is calculated, then left to right. If the longest length is ans, it encounters the first point output that satisfies len = ans, then ans--, records the last output point P, and then encounters len = ans point q, satisfies P.L >=q.l and P.R <= Q.R outputs the Q point position, then ans--and so on.


The method of calculating the longest ascending sub-sequence by divide and conquer.

------------------------------points have been sorted in reverse order.

When the [L,r] interval is encountered, the left interval is processed first,

Llllllrrrrrr

Then the ID value of R is reversed, R can only pass the past update Len value from L.

Save these points with a new array, press L to sort from small to large (in reverse order, so small to large), if L is the same then the ID value large row front.

Left-to-right enumeration:

Point P that encounters ID value > 0 inserts the Len value of the point in the segment tree P.R (whichever is maximum)

A point P query that encounters an ID value of < 0 satisfies the maximum value of Len in R>=Q.R, and +1 is the update len[p.id]

And then handle the right interval.

In doing so, R shifts from L first. If R is to be transferred from the R point, because the right interval is processed at the end, it is also calculated.

PS: Segment tree lazy mark, convenient situation, r to Discretization, otherwise the line tree can not save



#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Math.h> using namespace std;
#define MAXN 200007 int LZ[MAXN],LC[MAXN],RC[MAXN],VAL[MAXN];

int treecnt = 0;
    void build (int u,int l,int R) {if (L = = r) return;
    Lc[u] = treecnt++;
    Rc[u] = treecnt++;
    int mid = (l+r)/2;
    Build (Lc[u],l,mid);
Build (Rc[u],mid+1,r);
        } void Push_down (int u) {if (Lz[u]) {Lz[u] = 0;
        if (rc[u]! = 0) lz[rc[u]] = 1,val[rc[u]] = 0;
    if (lc[u]! = 0) lz[lc[u]] = 1,val[lc[u]] = 0;
    }} void update (int u) {val[u] = max (Val[rc[u]],val[lc[u]);} int query (int u,int l,int r,int pp) {push_down (U);
    if (l >= pp) return val[u];
    int mid = (l+r)/2;
    if (pp >= mid+1) return query (RC[U],MID+1,R,PP);
Return max (query (LC[U],L,MID,PP), query (RC[U],MID+1,R,PP));
        } void Add (int u,int l,int r,int pp,int num) {if (L = = r) {if (Lz[u]) val[pp] = 0; Lz[u] = 0;
        Val[u] = max (val[u],num);
    return;
    } push_down (U);
    int mid = (l+r)/2;
    if (pp >= mid+1) add (rc[u],mid+1,r,pp,num);
    else Add (lc[u],l,mid,pp,num);
Update (U);
} struct node{int l,r,id;};
Node P[MAXN];
Node Q[MAXN];

Node W[MAXN]; int Comp (node A,node b) {return A.R < B.R;} int comp1 (node A,node b) {return a.id > b.id;} int Comp3 (node
    A,node b) {if (a.l = = B.L) return a.id > b.id;
return A.L < B.L;
} int LEN[MAXN];
    void Fenzhi2 (int l,int r) {if (R < L) return;
    Sort (Q+l,q+r+1,comp3);
        for (int i = L;i <= r; i++) {int id = ABS (q[i].id);
        if (Q[i].id < 0) {Len[id] = max (Len[id],1+query (1,1,50000,Q[I].R));
        } else {Add (1,1,50000,q[i].r,len[id]);
    }} Lz[1] = 1;
VAL[1] = 0;
        } void Fenzhi (int l,int r) {if (L = = r) {Len[p[l].id] = max (len[p[l].id],1);
    return;
    } int mid = (l+r)/2; FenZhi (l,mid);
        for (int i = L;i <= r; i++) {q[i] = P[i];
    if (i > Mid) q[i].id *=-1;
    } fenzhi2 (L,r);
Fenzhi (MID+1,R);

} int comp2 (Node A,node b) {return a.id < b.id;}
    int main () {int n;
    Freopen ("1009.in", "R", stdin);
    Freopen ("10091.out", "w", stdout);
            while (scanf ("%d", &n)!=eof) {for (int i = 0;i < n;i++) {scanf ("%d", &AMP;P[I].L);
        P[i].id = i+1;
        } for (int i = 0;i < n; i++) {scanf ("%d", &AMP;P[I].R);
        } sort (P,p+n,comp);
        int cnt = 1,R = P[0].R;
            for (int i = 0;i < n; i++) {if (P[I].R = = r) P[I].R = CNT;
                else {r = P[I].R;
            P[I].R = ++cnt;

        }} sort (P,P+N,COMP1);
        memset (len,0,sizeof (len));
        memset (lz,0,sizeof (LZ));
        Memset (Val,0,sizeof (Val));
        Memset (Lc,0,sizeof (LC));
        memset (rc,0,sizeof (RC));
  treecnt = 2;      Build (1,1,50000);

        Fenzhi (0,n-1);
        int ans = 0;
        for (int i = 1;i <= n; i++) {ans = max (ans,len[i]);
        } sort (P,P+N,COMP2);
        Node x;
        int flag = 1,be=-1;
        printf ("%d\n", ans); for (int i = 0;i < n; i++) {if (ans = = Len[p[i].id]) {if (is = =-1 | | (P[I].L <= p[be].l && p[i].r >= P[BE].R))
                    {if (flag = = 0) printf ("");
                    printf ("%d", p[i].id);
                    Flag = 0;
                    ans--;
                be = i;
    }}} printf ("\ n");
} return 0;
 }










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.