题干:

Given a sequence of numbers A=a1,a2,…,aNA=a1,a2,…,aN, a subsequence b1,b2,…,bkb1,b2,…,bk of AA is referred as increasing if b1<b2<…<bkb1<b2<…<bk. LY has just learned how to find the longest increasing subsequence (LIS). 
Now that he has to select LL consecutive numbers and remove them from AA 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 TT indicating the number of test cases (T≤100T≤100). 
For each test case, the first line consists of two numbers NN and LL as described above (1≤N≤100000,0≤L≤N1≤N≤100000,0≤L≤N). The second line consists of NN integers indicating the sequence. The absolute value of the numbers is no greater than 109109. 
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”. XX is the test case number starting from 1. YY 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

题目大意:

每组样例给出两个整数N和L,然后给你N个数的序列,问删除连续的L个数之后,剩下的数的LIS的长度。

解题报告:

维护两个数组pre[i]和suf[i],分别代表从前往后以i为结尾的LIS和从后往前以i为第一个数的LIS。然后权值线段树维护离散化后的以每个值做开头,一直到结尾的最长LIS长度,然后查询的时候就枚举断点就好了。但是只有后缀的情况在此时会枚举不到,所以要单独枚举一下。但是只有前缀的情况是可以考虑到的不需要特判了。

注意预处理数组的时候需要单独开变量记录长度,因为数组中记录的必须是以当前点为结尾的。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1e5 + 5;
int n,L,a[MAX],b[MAX],LEN;
int get(int x) {return lower_bound(b+1,b+LEN+1,x) - b;
}
int suf[MAX],pre[MAX],tmp[MAX];
struct TREE {int l,r,mx;
} tr[MAX<<2];
void pushup(int cur) {tr[cur].mx = max(tr[cur*2].mx,tr[cur*2+1].mx);
}
void build(int l,int r,int cur) {tr[cur].l = l,tr[cur].r = r;tr[cur].mx = 0;if(l == r) return;int m = l+r >>1;build(l,m,cur*2);build(m+1,r,cur*2+1);
}
void update(int tar,int val,int cur) {if(tr[cur].l == tr[cur].r) {tr[cur].mx = max(tr[cur].mx,val);return;} if(tar <= tr[cur*2].r) update(tar,val,cur*2);else update(tar,val,cur*2+1);pushup(cur);
}
int query(int pl,int pr,int cur) {if(pl > pr) return 0;if(pl <= tr[cur].l && pr >= tr[cur].r) return tr[cur].mx;int res = 0;if(pl <= tr[cur*2].r) res = max(res,query(pl,pr,cur*2));if(pr >= tr[cur*2+1].l) res = max(res,query(pl,pr,cur*2+1));return res;
}
int my_lower_bound(int l,int r,int x) {int ans=r+1,mid;while(l<=r) {mid = l+r >>1;if(tmp[mid] < x) ans = mid,r = mid-1;else l = mid+1; } return ans;
}
int main()
{int T,iCase=0;cin>>T;while(T--) {scanf("%d%d",&n,&L);for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i];sort(b+1,b+n+1); LEN = unique(b+1,b+n+1) - b - 1;int len = 0;for(int i = 1; i<=n; i++) {int pos = lower_bound(tmp+1,tmp+len+1,a[i]) - tmp;pre[i] = pos;if(pos > len) len = pos;tmp[pos] = a[i];} suf[n+1] = 0; len = 0;for(int i = n; i>=1; i--) {int pos = my_lower_bound(1,len,a[i]);suf[i] = pos;if(pos > len) len = pos;tmp[pos] = a[i];}build(1,n,1);int ans = 0;for(int i = L+1; i<=n; i++) ans = max(ans,suf[i]);for(int i = n-L; i>=1; i--) {//枚举被删除的数字的起点 ans = max(ans,pre[i]+query(get(a[i])+1,n,1));update(get(a[i+L]),suf[i+L],1);}printf("Case #%d: %d\n",++iCase,ans);}return 0 ;
}

【HDU - 5489】Removed Interval(离散化,权值线段树,思维,最长上升子序列)相关推荐

  1. 权值线段树小结(hdu多校,普通平衡树,郁闷的出纳员)

    之前刷了一点主席树的题目,但是没有系统的做过权值线段树的题目.主席树是多根权值线段树的综合.权值线段树可以解决在总区间里求第k大的问题.在普通的线段树里,我们每一个节点维护的是权值大小.但是在权值线段 ...

  2. 2019CCPC网络赛 1002 HDU 6703(权值线段树)

    2019CCPC网络赛 1002 HDU 6703(权值线段树) 思路:用权值线段树存题目给的数据后,2操作就是求权值线段树中大于等于k的部分中,靠近左端点的第一个大于r的值(这个求出来的只是原序列中 ...

  3. Minimum Inversion Number HDU - 1394(权值线段树/树状数组)

    The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that s ...

  4. 2019牛客多校第七场E Find the median 权值线段树+离散化

    Find the median 题目链接: https://ac.nowcoder.com/acm/contest/887/E 题目描述 Let median of some array be the ...

  5. 【bzoj2770】YY的Treap 权值线段树

    题目描述 志向远大的YY小朋友在学完快速排序之后决定学习平衡树,左思右想再加上SY的教唆,YY决定学习Treap.友爱教教父SY如砍瓜切菜般教会了YY小朋友Treap(一种平衡树,通过对每个节点随机分 ...

  6. Zju2112 Dynamic Rankings(树状数组套可持久化权值线段树)

    Zju2112 Dynamic Rankings description solution code description 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须 ...

  7. 【BZOJ2653】middle,主席树(非权值线段树)维护序列和信息+二分答案

    传送门 写在前面:虽然这是一道我再也不想写的题目,但很好很有价值 思路: cxlove大神: 要求中位数最大,首先二分中位数,然后判断可行不可行. 判断X可行不可行,对于区间内的数,凡是>=X的 ...

  8. bzoj4605: 崂山白花蛇草水 权值线段树套KDtree

    bzoj4605: 崂山白花蛇草水 链接 bzoj loj 思路 强制在线,那就权值线段树套KDtree好了,没啥好讲的. KDtree要加平衡因子来重构.另外,那水真难喝. 错误 树套树一边写过了, ...

  9. 2019暑期训练——牛客第七场 C. Governing sand(权值线段树)

    Governing sand 链接: https://ac.nowcoder.com/acm/contest/887/C 题意: 给n种树,其中每一种树都有高度h,每砍掉一棵树所需要的代价c,这种树的 ...

最新文章

  1. git user name is not defined
  2. Python3中迭代器介绍
  3. JS中根据某个值进行大小排序
  4. Android Retrofit使用教程(三):Retrofit与RxJava初相逢
  5. python win10还是linux_在win10的Linux子系统(WSL)上搭载python编程环境
  6. url映射 ccf (Java正则表达式80分解法)
  7. python中面向对象_简述Python中的面向对象编程的概念
  8. 二、nodemon-Node.js 监控工具
  9. Golang生成C动态库.so和静态库.a
  10. python模块的使用方法_python中requests模块的使用方法
  11. Windows Server 2008 R2 主域控制器委派DNS到子域控控制器
  12. 解决向github提交代码不用输入帐号密码
  13. hdu1004(c++)
  14. 试题 算法训练 印章
  15. Excel 2010 VBA 入门 126 批量设置控件属性
  16. 毕设-基于SpringBoot宠物医院管理系统
  17. 微信小程序地图组件去除iconPath
  18. 深度:那些梦碎乐视的造车高人!
  19. 将 hexo 部署到云服务器
  20. 适合购买免备案云服务器一般是哪些网站业务?

热门文章

  1. 函数实现-aoti-atol
  2. 【转贴】利用 Javascript 获取 URL 参数(适合IE、FF)
  3. [剑指offer]面试题第[1]题[JAVA][二维数组中的查找][数组][二分]
  4. 【应用】Properties类与Properties配置文件的读写
  5. cad怎么把图层英文变成中文_CAD图层管理器昨天是中文的今天怎么变英文 – 手机爱问...
  6. python画建筑分析图_教你用GH绘制酷炫的流线分析图
  7. java 新窗口跳转页面_Java web开发中页面跳转小技巧——跳转后新页面在新窗口打开...
  8. python django部署docker_centos利用docker部署django项目
  9. c语言代码可以python运行吗_c语言如何运行python脚本
  10. mysql workbench中文设置_使用Workbench完成流体压力渗透分析