2015 Multi-University Training Contest 3 hdu 5324 Boring Class

Boring Class

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 900    Accepted Submission(s): 247


Problem Description

Mr. Zstu and Mr. Hdu are taking a boring class , Mr. Zstu comes up with a problem to kill time, Mr. Hdu thinks it’s too easy, he solved it very quickly, what about you guys?
Here is the problem:
Give you two 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 are many longest subsequence satisfy the condition, output the sequence which has the smallest lexicographic order.

Input
There are several test cases, each test case begins with an integer n.
1≤n≤50000
Both of the following two lines contain n integers describe the two sequences.
1≤Li,Ri≤109

Output
For each test case ,output the an 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
 

Author
ZSTU
 

Source

 解题:CDQ分治

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 200010;
 4 struct Node {
 5     int l,r,id;
 6     bool operator<(const Node &t) const {
 7         if(r != t.r) return r < t.r;
 8         if(l != t.l) return l > t.l;
 9         return id < t.id;
10     }
11 } P[maxn],A[maxn],B[maxn];
12 int n,tot,Li[maxn],C[maxn],dp[maxn];
13 void update(int i,int val) {
14     for(; i <= tot; i += i&(-i))
15         C[i] = max(C[i],val);
16 }
17 void clr(int i) {
18     for(; i <= tot; i += i&(-i)) C[i] = 0;
19 }
20 int query(int i) {
21     int ret = 0;
22     for(; i > 0; i -= i&(-i)) ret = max(ret,C[i]);
23     return ret;
24 }
25 void cdq(int L,int R) {
26     if(L == R) {
27         dp[P[L].id] = max(dp[P[L].id],1);
28         return;
29     }
30     int mid = (L + R)>>1;
31     cdq(mid+1,R);
32     int a = 0,b = 0;
33     for(int i = L; i <= mid; ++i) A[a++] = P[i];
34     for(int i = mid+1; i <= R; ++i) B[b++] = P[i];
35     sort(A,A+a);
36     sort(B,B+b);
37     int j = b-1;
38     for(int i = a-1; i >= 0; --i) {
39         for(; j >= 0 && B[j].r >= A[i].r; --j)
40             update(B[j].l,dp[B[j].id]);
41         dp[A[i].id] = max(dp[A[i].id],query(A[i].l) + 1);
42     }
43     for(int i = 0; i < b; ++i) clr(B[i].l);
44     cdq(L,mid);
45 }
46 int main() {
47     while(~scanf("%d",&n)) {
48         memset(dp,0,sizeof dp);
49         memset(C,0,sizeof C);
50         for(int i = tot = 0; i < n; ++i) {
51             scanf("%d",&P[i].l);
52             P[i].id = i;
53             Li[tot++] = P[i].l;
54         }
55         for(int i = 0; i < n; ++i) {
56             scanf("%d",&P[i].r);
57             Li[tot++] = P[i].r;
58         }
59         sort(Li,Li + tot);
60         tot = unique(Li, Li + tot) - Li;
61         for(int i = 0; i < n; ++i) {
62             P[i].l = lower_bound(Li,Li+tot,P[i].l) - Li + 1;
63             P[i].r = lower_bound(Li,Li+tot,P[i].r) - Li + 1;
64         }
65         cdq(0,n-1);
66         int ret = 0,pre = -1;
67         for(int i = 0; i < n; ++i) ret = max(ret,dp[i]);
68         printf("%d
",ret);
69         for(int i = 0; i < n; ++i) {
70             if(dp[i] == ret && (pre == -1 || P[i].l <= P[pre].l && P[i].r >= P[pre].r)) {
71                 if(pre != -1) putchar(' ');
72                 printf("%d",1 + i);
73                 --ret;
74                 pre = i;
75             }
76         }
77         puts("");
78     }
79     return 0;
80 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4725636.html