LIS问题(DP解法)---poj1631
题目链接:http://poj.org/problem?id=1631
这个题题目有些难看懂hhh,但实质就是求LIS--longest increasing sequence。
以下介绍LIS的解法模板:
一.O(n^2)解法
用a数组存储数据,f[i]表示以a[i] 结尾的最长子序列的长度,这样max(f[i])就是所求结果。
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std; int n,p;
int a[],f[];
int res; int main(){
scanf("%d",&n);
while(n--){
res=;
scanf("%d",&p);
for(int i=;i<=p;i++)
scanf("%d",&a[i]),f[i]=;
for(int i=;i<=p;i++)
for(int j=;j<i;j++)
if(a[j]<a[i]){
f[i]=max(f[i],f[j]+);
if(f[i]>res) res=f[i];
}
printf("%d\n",res);
}
return ;
}
但是算法复杂度为n2,题目p为4e4,还是多组询问,明显会超时。
二 . O(nlogn)解法
假设数字序列为a[N](也可不用保存,一边读入一边处理),我们会用到一个数组f[N],f[k]保存的是数组a中所有长为k的不下降子序列最后一个元素的最小值(下面将简称为最小最后元素),显然f的长度len即为所求。而且容易用反证法证明这个数组是递增的,若存在i<j,且f[i]>f[j],可以这样想,既然存在一个长为j的且最后一个元素为f[j]的不下降子串,则必然存在一个长为i(i<j)且最后一个元素为f[j]的不下降子串,所以f[i]<=f[j],与假设矛盾。初始化的时候,f中只有一个元素,即数字序列的第一个元素,显然f[1]=a[1],接着,每次读入一个数字时,相当数组a新增一个元素,我们设为tmp,我们的任务就是维持f数组的定义,具体操作如下:若tmp>f[len],则f[++len]=tmp,这个容易理解,若tmp<=f[1],则f[1]=tmp,这两种情况都容易理解,关键在于tmp>f[1]&&tmp<=f[len],这时需将f数组中第一个(从1到len)大于tmp的数字更新为tmp(这么做的原因是为了使后面输入时形成更长的上升子序列,不理解的话可以举个例子好好想想),因为f是有序的,所以可以使用二分查找,然后更新。当a数组确定后(数据读入完成),数组f也就确定了,此时数组f的长度就是数组a中以第一个数开头的最长不下降子序列的长度。读入数据使用一层循环,查找更新需一层循环,由于查找时用了二分,所以总的时间复杂度为O(nlogn)。至此算法就结束了。
AC代码如下:
#include<cstdio>
#include<algorithm>
using namespace std; int n,p;
int f[];
int len; int main(){
scanf("%d",&n);
while(n--){
len=;
scanf("%d",&p);
int tmp;
scanf("%d",&tmp);
f[++len]=tmp;
p--;
while(p--){
scanf("%d",&tmp);
if(tmp>f[len]) f[++len]=tmp;
else if(tmp<=f[]) f[]=tmp;
else{
int l=,r=len,m;
while(l<=r){
m=(l+r)/;
if(f[m]>=tmp) r=m-1;else l=m+;
}
f[l]=tmp;
}
}
printf("%d\n",len);
}
return ;
}
这个是求最长上升子序列。
求最长不下降子序列的代码如下:
#include<cstdio>
#include<algorithm>
using namespace std; int n,p;
int f[];
int len; int main(){
scanf("%d",&n);
while(n--){
len=;
scanf("%d",&p);
int tmp;
scanf("%d",&tmp);
f[++len]=tmp;
p--;
while(p--){
scanf("%d",&tmp);
if(tmp>=f[len]) f[++len]=tmp;
else if(tmp<f[]) f[]=tmp;
else{
int l=,r=len,m;
while(l<=r){
m=(l+r)/;
if(f[m]>tmp) r=m-;
else l=m+;
}
f[l]=tmp;
}
}
printf("%d\n",len);
}
return ;
}
LIS问题(DP解法)---poj1631的更多相关文章
- 小明的密码-初级DP解法
#include #include #include using namespace std; int visited[5][20][9009];// 访问情况 int dp[5][20][9009] ...
- hdu_4352_XHXJ's LIS(数位DP+状态压缩)
题目连接:hdu_4352_XHXJ's LIS 题意:这题花大篇篇幅来介绍电子科大的一个传奇学姐,最后几句话才是题意,这题意思就是给你一个LL范围内的区间,问你在这个区间内最长递增子序列长度恰为K的 ...
- luogu5010 HMR的LIS III (dp+线段树)
这个东西和最长上升子序列很像 考虑如果已经知道每个位置为开头的LIS长度和个数 f[i],我可以扫一遍 判断这个个数和K的大小,找到第一个长度=len而且个数<K的,这个位置就是要选的 然后K- ...
- HDU 4352 XHXJ's LIS 数位dp lis
目录 题目链接 题解 代码 题目链接 HDU 4352 XHXJ's LIS 题解 对于lis求的过程 对一个数列,都可以用nlogn的方法来的到它的一个可行lis 对这个logn的方法求解lis时用 ...
- HDU.4352.XHXJ's LIS(数位DP 状压 LIS)
题目链接 \(Description\) 求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\). \(Solution\) 数位DP. 至于怎么求LIS, ...
- XHXJ's LIS(数位DP)
XHXJ's LIS http://acm.hdu.edu.cn/showproblem.php?pid=4352 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4352 - XHXJ's LIS - [数位DP][LIS问题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- hdu 4352 XHXJ's LIS 数位dp+状态压缩
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others ...
- (探讨贴)POJ 1463 树形DP解法的不正确性
POJ1463是一个典型的树状DP题. 通常解法如下代码所示: using namespace std; ; ]; int pre[maxn]; int childcnt[maxn]; int n; ...
随机推荐
- [转]NSIS:常量大全
原文链接 http://www.flighty.cn/html/bushu/20140915_251.html ;轻狂志www.flighty.cn ;运行后会在桌面生成NSIS常量大全.txt文件 ...
- 全局 SqlConnection
class SqlHelper { public static SqlConnection conn; public static SqlConnection Open(string connStr) ...
- shell 11函数
函数定义 function 方法名(){ command return int; } 注意:function可加可不加 #shell #!/bin/sh function fun1(){ echo & ...
- cookie讲解
cookie:(翻译过来:小甜点) 意思是不管是谁都喜欢这个小东西 以谷歌为例: cookie:就是存放数据的东西,存放量(存储量很小,大约4KB)存放在客户端下,计算机上,应用设备上 应用场景:用户 ...
- 关于win时间同步的解决方案
将以下的批处理执行:net stop w32time sc config w32time start= auto net start w32time w32tm /config /update /ma ...
- springMVC 是单例还是的多例的?
曾经面试的时候有面试官问我spring的controller是单例还是多例,结果我傻逼的回答当然是多例,要不然controller类中的非静态变量如何保证是线程安全的,这样想起似乎是对的,但是不知道( ...
- Microsoft Dynamics CRM 2011 安装完全教程
作者:卞功鑫,转载请保留.http://www.cnblogs.com/BinBinGo/p/4302612.html 环境介绍 WINDOWS 2008 R2 Datacenter Microsof ...
- mongodb中查询返回指定字段
mongodb中查询返回指定字段 在写vue项目调用接口获取数据的时候,比如新闻列表页我只需要显示新闻标题和发表时间,点击每条新闻进入详情页的时候才会需要摘要.新闻内容等关于此条新闻的所有字段. ...
- c++官方文档-class
#include <iostream> using namespace std; class Circle { double radius; public: Circle(double r ...
- rabbitMQ 的基本知识
参考: https://www.cnblogs.com/dwlsxj/p/RabbitMQ.html