【动态规划+二分查找】POJ2533&POJ1631最长上升子序列(LIS)
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)的更多相关文章
- 【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】
二分 lower_bound lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址) upper_bound upper_bound()与lower_bound() ...
- 动态规划(DP),最长递增子序列(LIS)
题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...
- 最长回文子序列LCS,最长递增子序列LIS及相互联系
最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...
- 最长上升子序列LIS(51nod1134)
1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递 ...
- 一个数组求其最长递增子序列(LIS)
一个数组求其最长递增子序列(LIS) 例如数组{3, 1, 4, 2, 3, 9, 4, 6}的LIS是{1, 2, 3, 4, 6},长度为5,假设数组长度为N,求数组的LIS的长度, 需要一个额外 ...
- 2.16 最长递增子序列 LIS
[本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...
- 题解 最长上升子序列 LIS
最长上升子序列 LIS Description 给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的 ...
- 1. 线性DP 300. 最长上升子序列 (LIS)
最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submission ...
- 二维动态规划&&二分查找的动态规划&&最长递增子序列&&最长连续递增子序列
题目描述与背景介绍 背景题目: [674. 最长连续递增序列]https://leetcode-cn.com/problems/longest-continuous-increasing-subseq ...
随机推荐
- css文本垂直水平居中
一.单行文本居中 .content{ height:100px; line-height:100px; text-align:center; border:1px solid red; } 效果图 二 ...
- 浅谈JobExecutionContext与JobDataMap
1.JobExecutionContext简介 (1)当Scheduler调用一个Job,就会将JobExecutionContext传递给job的execute方法 quartz无法调用job的有参 ...
- 己动手创建最精简的Linux
己动手创建最精简的Linux http://blog.sina.com.cn/s/blog_71c87c170101e7ru.html 首次 LFS 搭建全过程 http://zmyxn.blog.5 ...
- System.getProperty方法中输出路径的方法
package codegenerator;/** *@author Eilen *@date 2017年9月27日---下午3:15:09 *@描述: *@answer */public class ...
- 4.Python3标准库--算法
(一)functools:管理函数的工具 import functools ''' functools模块提供了一些工具来管理或扩展和其他callable对象,从而不必完全重写 ''' 1.修饰符 f ...
- git学习笔记三
1.每个分支的历史版本维护信息位置是.git/logs/refs/heads/master,这个位置的信息是文本文件,不是引用. harvey@harvey-Virtual-Machine:~/dem ...
- Python 微信公众号发送消息
1. 公众号测试地址 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index 2. ...
- redis之(八)redis的有序集合类型的命令
[一]增加元素 --->命令:ZADD key score member [score member] --->向有序集合放入一个分数为score的member元素 --->元素存在 ...
- 深入理解计算机(CSAPP)资源汇总
用于资源记录. 视频: 卡内基梅隆大学 Introduction to Computer Systems CMU 15-213 Fall 作业(labs): Lab Assignments
- WordPress函数query_posts用法汇总
最近经常有网友跟我咨询WordPress函数query_posts的相关用法,说起来query_posts实在是太强大,参数无数,用法更是无数,如果让我说它的用法,我根本没法一一说清楚.开始之前,你可 ...