最长上升子序列1006
来源:互联网 发布:测试键盘按键软件 编辑:程序博客网 时间:2024/06/05 20:09
Removed Interval
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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 selectL 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?
Now that he has to select
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 numbersN 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.
For each test case, the first line consists of two numbers
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
25 21 2 3 4 55 35 4 3 2 1
Sample Output
Case #1: 3Case #2: 1
思路:这题如果直接枚举删除的话再找出最长的上升子序列这样时间会超出,然后可以找出要删除的左右两边最大上升子序列之和,以代替枚举要删除的地方
之后的最长上升子序列。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#define INF 0x3f3f3fusing namespace std;int a[100005];int b[100005];int c[100005];int dp[100005];int main(){ int T; cin>>T; int t=1; while(T--) { int m,n; cin>>m>>n; int i,x; for(i=0;i<m;i++) { scanf("%d",&a[i]); b[i]=-a[i]; } memset(c, 0x7f, sizeof(c)); for(i=m-1;i>=0;i--) { x=lower_bound(c,c+m,b[i])-c; c[x]=b[i]; dp[i]=x+1; } memset(c, 0x7f, sizeof(c)); int zz=0,len=0; for(i=n;i<m;i++) { x=lower_bound(c,c+m,a[i])-c; zz=max(zz,x+1+dp[i]-1); x=lower_bound(c,c+m,a[i-n])-c; c[x]=a[i-n]; len=max(len,x+1); } printf("Case #%d: ", t++); if(zz>len)len=zz; cout<<len<<endl; } return 0;}
阅读全文
0 0
- 最长上升子序列1006
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 基于Maven的Springboot项目搭建学习笔记
- Java泛型-类型擦除
- 【JZOJ4425】【HNOI2016模拟4.4】Fenwit
- Java并发之并行与并发的区别
- ShuffleNet算法详解
- 最长上升子序列1006
- Openstack4J API初体验之删除一个实例(删除一个虚拟机)
- Crazy Learning for Day 14
- 数学之美番外篇:平凡而又神奇的贝叶斯方法
- 高仿淘宝购物车分分钟让你集成
- Go 语言的Template Package
- Python高效编程的19个技巧
- 视觉里程计(三)
- BZOJ 1935 Tree 园丁的烦恼 [差分][树状数组]