最长上升子序列
来源:互联网 发布:网络卫星电视机顶盒 编辑:程序博客网 时间:2024/06/04 23:33
1209: 最长上升子序列
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 19 Solved: 10
[Submit][Status][Web Board]
Description
一个数的序列ai,当a1 < a2 < ... < aS的时候,我们称这个 序列是上升的。对于给定的一个序列(a1, a2, ..., aN),我们可 以得到一些上升的子序列(ai1, ai2, ..., aiK),这里1 <= i1 < i2 < ... < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8), 有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序 列中最长的长度是4,比如子序列(1, 3, 5, 8). 你的任务,就是对于给定的序列,求出最长上升子序列的长度。
Input
输入的第一行是序列的长度N (1 <= N <= 1000)。第二行给出 序列中的N个整数,这些整数的取值范围都在0到10000。
Output
最长上升子序列的长度。
Sample Input
7 1 7 3 5 9 4 8
Sample Output
4
简单的动态规划
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>usingnamespace std; intmain(){ intdp[2000],a[2000],n; while(cin>>n) { memset(dp,0,sizeof(dp)); intres = 0; for(inti = 0; i < n; i++) cin>>a[i]; for(inti = 0; i < n; i++) { dp[i] = 1; for(intj = 0; j < i; j++) { if(a[j] < a[i]) dp[i] = max(dp[i],dp[j] + 1); } res = max(res,dp[i]); } cout<<res<<endl; } return0;}
下面给出O(nlogn)的算法
#include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int INF = 9999990;int a[100001],dp[100001];int main(){ #ifdef xxz freopen("in.txt","r",stdin); #endif // xxz int n,T; scanf("%d",&T); for(int ca = 1; ca <= T; ca++){ scanf("%d",&n); for(int i = 0; i < n; i++) scanf("%d",a+i); fill(dp,dp + n, INF); for(int i = 0; i < n; i++){ *lower_bound(dp,dp+n,a[i]) = a[i]; } printf("%d\n",lower_bound(dp,dp+n,INF) - dp); } return 0;}
如果要求非严格递增的话
*lower_bound(dp,dp+n,a[i]) = a[i];改成*upper_bound()就行
0 0
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 最长上升子序列
- 基于形状的模板匹配
- java_servlet_jsp
- 【BZOJ2282】【Sdoi2011】消防 树的直径+双指针+单调队列 有一系列乱七八糟的证明
- C语言经典面试题
- uva 10534 Wavio Sequence (最长上升子序列)
- 最长上升子序列
- php 20道难题
- 蓝桥杯 猜生日
- 三层---人员管理系统
- 选项菜单的应用
- 【Objective-C学习笔记】基本语法、类与对象
- 利用OpenCV的Haar特征目标检测方法进行人脸识别的尝试(一)
- 自定义类的对象作为TreeSet元素的两种方法排序浅谈
- iOS开发之如何访问通讯录