有关最长上升子序列的详细算法解释在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. js变量和数据类型

  2. Vue的生命周期(在其他地方看到一份非常好又详细的详解)

    链接地址:https://segmentfault.com/a/1190000011381906 首先,每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期.首先看一张图 ...

  3. 007/Docker(一)

    Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口. Docker 将应用程序与该程序的依赖,打包在一个文件里面.运行这个文件,就会生成一个虚拟容器.程序在这个虚拟容器里运行, ...

  4. dataTables使用的详细说明整理

    本文共三个部分:官网|基本使用|遇到的问题 一.官方网站:http://www.datatables.club/ 二.基本使用: 1.dataTables的引入及初始化 <!--第一步:引入Ja ...

  5. redis关闭和启动

    redis关闭 到redis节点目录下执行如下命令 redis-cli -p 端口号 shutdown redis启动 ./redis-server 参数 参数:redis.conf文件全路径 需要到 ...

  6. hihocoder1954 : 压缩树

    传送门 首先求出缩一个点 $x$ 的贡献,就是缩 $x$ 的父亲的贡献加上 $x$ 的子树多减少的深度 假设此时缩父亲的贡献已经考虑过了,那么 $x$ 的子树多减少的深度就是子树的节点数 注意此时要满 ...

  7. overflow的量两种模式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JavaScript基础5——动态显示时间

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. python 安装 pip ,并使用pip 安装 filetype

    闲话少说,直接上操作. python版本为2.7.6 可以直接到官网下载,我也提供一个百度云的下载地址 https://pan.baidu.com/s/1kWPXG8Z 这个是window版本,lin ...

  10. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) - C

    题目链接:http://codeforces.com/contest/831/problem/C 题意:给定k个评委,n个中间结果. 假设参赛者初始分数为x,按顺序累加这k个评委的给分后得到k个结果, ...