有关最长上升子序列的详细算法解释在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. unity编辑器Hierarchy添加图标

    效果 素材 using UnityEditor; using UnityEngine; using System.Collections.Generic; [InitializeOnLoad] cla ...

  2. ArrayList,linkedList vecator的实现和区别

    1.线程安全问题. ArrayList 和 linkedList 线程是不安全的,而vecator是线程安全的. 因为ArrayList 和 linkedList 是线程不同步的,vecator是同步 ...

  3. 兼容IE浏览器保存Cookie

    兼容IE:Response.Cookies[":member"].Expires=DateTime.Now.AddDays(1); 其它浏览器:Response.Cookies[& ...

  4. 链表两数相加(add two numbers)

    问题 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它 ...

  5. “希希敬敬对”团队——敏捷冲刺Alpha过程总结

    “希希敬敬对”团队在七天冲刺过程中每一个小组成员都尽力去完成自己的任务.在合作过程中,总算是有一些成果出现,代码功能能够实现. 对此次冲刺有如下优缺点: 优点: 团队人员合作较多,成员都能够积极响应参 ...

  6. [Web 前端] 008 css 颜色表示方法

    css 颜色表示法 颜色名表示 如 red 红色 green 绿色 blue 蓝色 16 进制数值表示 常见颜色 正常表示 缩写表示 红色 #ff0000 #f00 绿色 #00ff0 #0f0 蓝色 ...

  7. jenkins使用记录转自https://my.oschina.net/sanpeterguo/blog/197931

    摘要: jenkins(持续集成开源工具)提供了丰富的api接口,基本上所有的操作都可以使用curl来从后台调度,包括:创建项目,禁用项目,启用项目,获取项目描述,获取配置文件,普通触发,scm触发, ...

  8. 11-jQuery简介和选择器

    # jQuery > jQuery是一个是免费.开源的javascript库, 也是目前使用最广泛的javascript函数库.>> jQuery极大的方便你完成web前段的相关操作 ...

  9. 跨域问题解决----NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost:11000' is therfore not allowed access'

    NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost ...

  10. 一gradle创建SSM项目——依赖包

    build.gradle compile:编译时必须. runtime:运行时必须,包括编译时. testCompile:测试编译时必须. testRuntime:测试运行时必须,包括编译时. 注:此 ...