有关最长上升子序列的详细算法解释在http://www.cnblogs.com/denghaiquan/p/6679952.html

1)51nod 1134

  一题裸的最长上升子序列,由于N<=50000,n2算法会超时,只能用nlogn算法。

 

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const LL mod = +;
int a[maxn];
int dp[maxn];
int Binary_search(int key, int len)
{
int l=, r=len+;
while(l<r)
{
int middle = (l+r) >> ;
if(key>=dp[middle])
l = middle +;
else
r = middle;
}
return l;
}
int main()
{
#ifdef LOCAL
freopen("input.txt" , "r", stdin);
#endif // LOCAL
int n, len;
scanf("%d", &n);
for(int i=;i<n;i++) scanf("%d", &a[i]);
dp[] = a[];
len = ;
for(int i=;i<n;i++)
{
if(a[i] > dp[len])
dp[++len] = a[i];
else{
int j = Binary_search(a[i], len);
dp[j] = a[i];
}
}
printf("%d\n", len);
return ;
}

2)POJ 2533

  裸的最长上升子序列,N<=1000, n2算法可以过。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const LL mod = +;
int a[maxn];
int dp[maxn];
int main()
{
#ifdef LOCAL
freopen("input.txt" , "r", stdin);
#endif // LOCAL
int n;
scanf("%d", &n);
for(int i=;i<n;i++) scanf("%d", &a[i]);
dp[]=;
for(int i=;i<n;i++)
{
dp[i] = ;
for(int j=;j<i;j++)
{
if(a[j]<a[i] && dp[j]+ > dp[i])
dp[i] = dp[j] + ;
}
}
int ans = ;
for(int i=;i<n;i++) ans = max(ans, dp[i]);
printf("%d\n", ans);
return ;
}

3)POJ 1631

  一题裸的最长上升子序列,由于N<=50000,n2算法会超时,只能用nlogn算法。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const LL mod = +;
int a[maxn];
int dp[maxn];
int Binary_search(int key, int len)
{
int l=, r=len+;
while(l<r)
{
int middle = (l+r) >> ;
if(key>=dp[middle])
l = middle +;
else
r = middle;
}
return l;
}
int main()
{
#ifdef LOCAL
freopen("input.txt" , "r", stdin);
#endif // LOCAL
int T;
scanf("%d", &T);
while(T--)
{
ms(dp, );
ms(a, );
int n, len;
scanf("%d", &n);
for(int i=;i<n;i++) scanf("%d", &a[i]);
dp[] = a[];
len = ;
for(int i=;i<n;i++)
{
if(a[i] > dp[len])
dp[++len] = a[i];
else{
int j = Binary_search(a[i], len);
dp[j] = a[i];
}
}
printf("%d\n", len);
}
return ;
}

4)POJ 1887

  这题是找最长下不上升子序列。发现n2算法可以过。不过要注意输出。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const LL mod = +;
int a[maxn];
int dp[maxn];
int main()
{
#ifdef LOCAL
freopen("input.txt" , "r", stdin);
#endif // LOCAL
int t=;
while(scanf("%d", &a[])&&a[]!=-)
{
if(t>) printf("\n");
int n =, ans = ;
while(scanf("%d", &a[n])&&a[n]!=-) n++;
dp[] = ;
for(int i=;i<n;i++)
{
dp[i] = ;
for(int j=;j<n;j++)
{
if(a[j] > a[i] && dp[j]+ > dp[i])
dp[i] = dp[j] + ;
}
}
for(int i=;i<n;i++) ans = max(ans, dp[i]);
printf("Test #%d:\n",t++);
printf(" maximum possible interceptions: %d\n", ans);
ms(a, );
ms(dp, );
}
return ;
}

5)POJ 1609

  这题同样是最长不上升子序列,不过是2个关键词,先排序一个,再在另一个里面找最长不上升子序列。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const LL mod = +;
int a[maxn];
int dp[maxn];
struct node
{
int l, m;
};
bool cmp(node x1, node x2)
{
if(x1.l != x2.l)
return x1.l > x2.l;
return x1.m > x2.m;//当第一个关键字相同的时候,第2个关键字要从大到小排序
}
int main()
{
#ifdef LOCAL
freopen("input.txt" , "r", stdin);
#endif // LOCAL
int n;
while(scanf("%d", &n))
{
if(n == ) {printf("*\n");break;}
node blocks[maxn];
for(int i = ;i < n;i++) scanf("%d%d", &blocks[i].l, &blocks[i].m);
sort(blocks, blocks+n, cmp);
dp[] = ;
for(int i=;i<n;i++)
{
dp[i] = ;
for(int j = ;j<i;j++)
{
if(blocks[j].m >= blocks[i].m && dp[j] + > dp[i])
dp[i] = dp[j]+;
}
}
int ans = ;
for(int i=;i<n;i++) ans = max(ans, dp[i]);
printf("%d\n", ans);
ms(dp, );
}
return ;
}

 

最长上升子序列(LIS)题目合集的更多相关文章

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

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

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

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

  3. 2.16 最长递增子序列 LIS

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

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

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

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

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

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

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

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

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

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

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

  9. 【CJOJ2498】【DP合集】最长上升子序列 LIS

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

随机推荐

  1. LeetCode 算法 Part 1

    目录 1. 两数之和 1. 题目 2.代码 4. 算法用时 5. 感想 2. 两数相加 1. 题目 2.代码 4. 算法用时 5. 感想 3. 无重复字符的最长子串 1. 题目 2.代码 4. 算法用 ...

  2. 浅谈WebService开发二(同步与异步调用)转

    上文 <http://www.dotnetgeek.cn/xuexiwebservice1.html>已经跟大家说了,如果创建一个webservice和简单的调用,本文将注重webserv ...

  3. 2019寒假作业二:PTA7-1币值转换

    7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成“贰万叁仟壹百零捌”元.为了简化输出,用小写英文字 ...

  4. VSphere服务器ESXI4.1.0设置虚拟主机来电开机自启动

    vSphere服务器ESXI设置虚拟主机来电自启动 首先查看我自己VMware vSphere版本为4.1.0(需要在虚拟主机电源为关闭状态下编辑) 然后双击主机,点击配置---虚拟机启动/关机 点击 ...

  5. cJSON使用笔记

    将cJSON用到STM32f103上 cJSON内存管理使用的是标准库stdlib.h的malloc()free()realloc()动态内存管理函数,我STM32F103使用的是正点原子的mallo ...

  6. CentOS7 开机启动流程

  7. 《剑指offer》面试题19 二叉树的镜像 Java版

    书中方法:这道题目可能拿到手没有思路,我们可以在纸上画出简单的二叉树来找到规律.最后我们发现,镜像的实质是对于二叉树的所有节点,交换其左右子节点.搞清楚获得镜像的方法,这道题实际上就变成了一道二叉树遍 ...

  8. 移动端1像素解决方法,根据媒体查询transform缩放

    .borderOnePx{ position: relative; } .borderOnePx::after { content: ''; height:1px; background:#000; ...

  9. 2019 Multi-University Training Contest 2 - 1008 - Harmonious Army - 最大流

    http://acm.hdu.edu.cn/showproblem.php?pid=6598 一开始就觉得是网络流,但是一直都不会怎么建图. 这里要考虑. 每一组边(u,v,a,b,c)建立如下的连接 ...

  10. 如何设置 ComboBox 下拉列表的高度或间距

    ComboBox 的下拉列表部分总是很挤,看起不舒服,但是设置了 ItemHeight 没用,怎么办呢? 首先设置一个较大的 ItemHeight 值,比如 20: 然后设置 ComboBox 的 D ...