POJ2533裸的LIS,时间复杂度为O(n^2)

 #include<iostream>
#include<cstdio>
using namespace std;
const int MAXN=+;
int a[MAXN];
int dp[MAXN];
int n,ans; int main()
{
scanf("%d",&n);
for (int i=;i<n;i++)
{
scanf("%d",&a[i]);
dp[i]=;
}
ans=-;
for (int i=;i<n;i++)
{
for (int j=;j<i;j++)
if (a[j]<a[i] && dp[j]+>dp[i])
{
dp[i]=dp[j]+;
}
if (dp[i]>ans)
{
ans=dp[i];
}
}
cout<<ans<<endl;
return ;
}

POJ1631

两条线路i与j不交叉的前提条件是a[i]<a[j],即上升子序列。用二分搜索+LIS,时间复杂度为O(n^2),具体解释详见《挑战程序设计竞赛2.3记录结果在利用的“动态规划”》P65

 #include<iostream>
#include<cstdio>
using namespace std;
const int MAXN=+;
const int INF=+;
int a[MAXN];
int dp[MAXN];//dp[i]表示长度为i+1的上升子序列末位元素的最小值
int n,m,ans,l,r; int search(int k)
{
int ul=l,ur=r;
while (ur-ul>)
{
int mid=(ur+ul)/;
if (dp[mid]>=k) ur=mid;
else ul=mid;
}
return ur;
} int main()
{
scanf("%d",&m);
for (int kase=;kase<m;kase++)
{
scanf("%d",&n);
for (int i=;i<n;i++)
{
scanf("%d",&a[i]);
dp[i]=INF;
}
l=-;
r=;
for (int i=;i<n;i++)
{
int pos=search(a[i]);
dp[pos]=a[i];
if (pos==r) r++;
}
cout<<r<<endl;
}
return ;
}

【动态规划+二分查找】POJ2533&POJ1631最长上升子序列(LIS)的更多相关文章

  1. 【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】

    二分 lower_bound lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址) upper_bound upper_bound()与lower_bound() ...

  2. 动态规划(DP),最长递增子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...

  3. 最长回文子序列LCS,最长递增子序列LIS及相互联系

    最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...

  4. 最长上升子序列LIS(51nod1134)

    1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递 ...

  5. 一个数组求其最长递增子序列(LIS)

    一个数组求其最长递增子序列(LIS) 例如数组{3, 1, 4, 2, 3, 9, 4, 6}的LIS是{1, 2, 3, 4, 6},长度为5,假设数组长度为N,求数组的LIS的长度, 需要一个额外 ...

  6. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  7. 题解 最长上升子序列 LIS

    最长上升子序列 LIS Description 给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的 ...

  8. 1. 线性DP 300. 最长上升子序列 (LIS)

    最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submission ...

  9. 二维动态规划&&二分查找的动态规划&&最长递增子序列&&最长连续递增子序列

    题目描述与背景介绍 背景题目: [674. 最长连续递增序列]https://leetcode-cn.com/problems/longest-continuous-increasing-subseq ...

随机推荐

  1. javascript性能

    1.js文件放在底部 js文件具有阻塞机制,放在头部,需要等待js下载解析完毕之后才能下载渲染页面,因此需要放在底部

  2. Tomcat8配置默认项目

    <!-- 配置默认访问项目 --> <Host name="localhost" appBase="webapps" unpackWARs=& ...

  3. mysql增删

    create table msg (id int, name varchar(10)); 插入语句 insert into msg values(1,'root'); insert into msg( ...

  4. vs 2015 插件 supercharger 破解方式

    亲测有效:效果如图 方法如下: 1.打开Supercharger的options; 2.点击Pricing & Registration 3.复制 license  然后再按Paste &am ...

  5. android CVE 漏洞汇总

    arm exploits 技术教程: Learning Pentesting for Android Devices CVE-2015-1530 ,CVE-2015-1474 两个android整数溢 ...

  6. mybaits-spring demo 记

    代码:https://github.com/bobby96333/demo_spring_batis /pom.xml <?xml version="1.0" encodin ...

  7. 【bzoj4868】期末考试

    我还第一次见到省选考三分……? #include<bits/stdc++.h> #define N 200005 using namespace std; typedef long lon ...

  8. linux指令和文件系统

    linux root用户的主目录是 /root , 其余用户在 /home 中: tar 常用 tar -zxvf : 安装使用 yum or wget website: mv a.g b.g 重命名 ...

  9. 访问WEB-INF目录中的文件

    WEB-INF下面的文件具有安全性,直接在浏览器中输入地址是访问不到的,可以通过servlet来访问. 方法1: 利用<jsp-file>/WEB-INF/a.jsp</jsp-fi ...

  10. AspxGridView在cell内显示颜色

    protected void master_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if ...