hdu 5489(LIS最长上升子序列)
题意:一个含有n个元素的数组,删去k个连续数后,最长上升子序列 /*思路参考GoZy
思路: 4 2 3 [5 7 8] 9 11 ,括号表示要删掉的数,
所以 最长上升子序列 = ] 右边数A的lis + [左边最大值小于A的lis
即相当于枚举删除的所有情况,并求它们的LIS,取最大值
如本例 : 最长 = 2[ 9 11] + 2[2 3], 然后将框从左往右移,算出最大值
用nlog(n)求LIS:
对于a[i],在arr数组中用log(n)找到比它小的数的个数x,arr[x] = a[i] ,arr保存的到当前位置的最长LIS
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#define MAXN 100010
typedef long long ll;
using namespace std; const int N = 1e5 + 5;
int a[N];
int b[N];
int d1[N]; //表示i处的LIS
int arr[N]; int main()
{
int t,cas = 1;
int n,len;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&len);
for(int i = 0; i <= n-1; i++)
{
scanf("%d",&a[i]);
b[i] = -a[i]; //为求右边的LIS
} memset(arr,0x3f3f3f,sizeof(arr));
for(int i = n-1; i >= 0; i--) //nlog(n)求LIS
{
int x = lower_bound(arr,arr+n,b[i]) - arr; //用log(n)查找,也可二分
arr[x] = b[i];
d1[i] = x+1;
} int ans = 0,tlen = 0;
memset(arr,0x3f3f3f,sizeof(arr));
for(int i = len; i < n; i++) //arr中保存框左边的数最长lis
{
int x = lower_bound(arr,arr+n,a[i]) - arr; //在前面找最大值比a[i]小的最长LIS
ans = max(ans,x + d1[i]);
x = lower_bound(arr,arr+n,a[i - len]) - arr;
arr[x] = a[i - len];
tlen = max(x+1,tlen); //记录左边的最长长度
}
printf("Case #%d: ", cas++);
ans = max(ans,tlen); //比较全在框左边的情况
printf("%d\n",ans);
}
return 0;
}
ps.如果没有东西值得你为之努力,那你和一条咸鱼有什么区别?
hdu 5489(LIS最长上升子序列)的更多相关文章
- hdu 5256 序列变换(LIS最长上升子序列)
Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多 ...
- 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
- POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
- POJ 3903 Stock Exchange (E - LIS 最长上升子序列)
POJ 3903 Stock Exchange (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 动态规划模板1|LIS最长上升子序列
LIS最长上升子序列 dp[i]保存的是当前到下标为止的最长上升子序列的长度. 模板代码: int dp[MAX_N], a[MAX_N], n; int ans = 0; // 保存最大值 for ...
- POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)
POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...
- hdu 5748(求解最长上升子序列的两种O(nlogn)姿势)
Bellovin Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepte ...
- LIS最长上升子序列O(n^2)与O(nlogn)的算法
动态规划 最长上升子序列问题(LIS).给定n个整数,按从左到右的顺序选出尽量多的整数,组成一个上升子序列(子序列可以理解为:删除0个或多个数,其他数的顺序不变).例如序列1, 6, 2, 3, 7, ...
- LIS 最长递增子序列
一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...
随机推荐
- codevs 3981 动态最大子段和
3981 动态最大子段和 http://codevs.cn/problem/3981/ 题目等级 : 钻石 Diamond 题目描述 Description 题目还是简单一点好... 有n个 ...
- css中的position
一.position语法与结构 position语法: position : static absolute relative position参数:static : 无特殊定位,对象遵循HTML定位 ...
- 在windows环境下安装redis和phpredis的扩展
在windows环境下安装redis和phpredis的扩展 1.首先配置php: 需要在windows的集成环境中找到php的扩展文件夹,ext,然后在网上寻找自己的php对应的.dll文件 比如说 ...
- 常见web攻击总结
搞Web开发离不开安全这个话题,确保网站或者网页应用的安全性,是每个开发人员都应该了解的事.本篇主要简单介绍在Web领域几种常见的攻击手段及Java Web中的预防方式. XSS SQL注入 DDOS ...
- java中类的三大特征之多态
Java 多态 同一种事物由于条件不同,展示出不同的结果,叫做多态. 父类的引用类型,由于使用不同的子类对象实例,而执行不同的操作. 多态存在的三个必要条件 1. 子类继承父类: 2. 子类重写父类方 ...
- 阿里云API网关(9)常见问题
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- 新概念英语(1-39)Don't drop it!
新概念英语(1-39)Don't drop it! Where does Sam put the vase in the end ? A:What are you going to do with t ...
- OrientDB入门(1)Getting Started
Running OrientDB the First Time First, download and extract OrientDB by selecting the appropriate pa ...
- 【第十九篇】laydate设置起始时间,laydate设置开始时间和结束时间
laydate设置开始时间后,结束时间不可小于已选择的开始时间 laydate设置结束时间后,开始时间不可小于已选择的结束时间 //设置开始时间 var startDate = laydate.ren ...
- Python 爬虫性能相关
性能相关 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. import requests def fetch_async(url): ...