HDU 5489 Removed Interval

Removed Interval

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 73    Accepted Submission(s): 33

Problem Description
Given a sequence of numbers A=a1,a2,…,aN, a subsequence b1,b2,…,bk of A is referred as increasing if b1<b2<…<bk. LY has just learned how to find the longest increasing subsequence (LIS).
Now that he has to select L consecutive numbers and remove them from A for some mysterious reasons. He can choose arbitrary starting position of the selected interval so that the length of the LIS of the remaining numbers is maximized. Can you help him with this problem?

Input
The first line of input contains a number T indicating the number of test cases (T≤100).
For each test case, the first line consists of two numbers N and L as described above (1≤N≤100000,0≤L≤N). The second line consists of N integers indicating the sequence. The absolute value of the numbers is no greater than 109.
The sum of N over all test cases will not exceed 500000.

Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the maximum length of LIS after removing the interval.

Sample Input
2
5 2
1 2 3 4 5
5 3
5 4 3 2 1
 
Sample Output
Case #1: 3
Case #2: 1
 
Source
 
解题:LIS + 线段树
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100010;
 4 const int INF = 0x3f3f3f3f;
 5 int tree[maxn<<2],a[maxn],b[maxn],d[maxn],q[maxn],Lisan[maxn],n,m;
 6 void update(int lt,int rt,int pos,int val,int v){
 7     if(lt == rt){
 8         tree[v] = max(tree[v],val);
 9         return;
10     }
11     int mid = (lt + rt)>>1;
12     if(pos <= mid) update(lt,mid,pos,val,v<<1);
13     else update(mid + 1,rt,pos,val,v<<1|1);
14     tree[v] = max(tree[v<<1],tree[v<<1|1]);
15 }
16 int query(int L,int R,int lt,int rt,int v){
17     if(lt <= L && rt >= R) return tree[v];
18     int mid = (L + R)>>1;
19     int ret = 0;
20     if(lt <= mid) ret = query(L,mid,lt,rt,v<<1);
21     if(rt > mid) ret = max(ret,query(mid + 1,R,lt,rt,v<<1|1));
22     return ret;
23 }
24 int main() {
25     int kase,cs = 1;
26     scanf("%d",&kase);
27     while(kase--) {
28         scanf("%d%d",&n,&m);
29         memset(q,0x3f,sizeof q);
30         for(int i = 0; i < n; ++i) {
31             scanf("%d",d + i);
32             a[i] = lower_bound(q,q + n,d[i]) - q + 1;
33             q[a[i]-1] = d[i];
34             Lisan[i] = d[i];
35         }
36         sort(Lisan,Lisan + n);
37         int tot = unique(Lisan,Lisan + n) - Lisan;
38         memset(q,0x3f,sizeof q);
39         for(int i = n-1; i >= 0; --i) {
40             b[i] = lower_bound(q,q + n,-d[i]) - q + 1;
41             q[b[i]-1] = -d[i];
42         }
43         memset(tree,0,sizeof tree);
44         int ret = 0;
45         for(int i = m + 1; i <= n; ++i){
46             int pos = lower_bound(Lisan,Lisan + tot,d[i - 1]) - Lisan + 1;
47             ret = max(ret,query(0,tot,0,pos - 1,1) + b[i - 1]);
48             pos = lower_bound(Lisan,Lisan + tot,d[i - m - 1]) - Lisan + 1;
49             update(0,tot,pos,a[i - m - 1],1);
50         }
51         ret = max(ret,tree[1]);
52         printf("Case #%d: %d
",cs++,ret);
53     }
54     return 0;
55 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4842519.html