1134 最长递增子序列
2016-08-11 21:05
267 查看
1134 最长递增子序列
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
给出长度为N的数组,找出这个数组的最长递增子序列。(递增子序列是指,子序列的元素是递增的)
例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10。
Input
Output
Input示例
Output示例
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
给出长度为N的数组,找出这个数组的最长递增子序列。(递增子序列是指,子序列的元素是递增的)
例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10。
Input
第1行:1个数N,N为序列的长度(2 <= N <= 50000) 第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= S[i] <= 10^9)
Output
输出最长递增子序列的长度。
Input示例
8 5 1 6 8 2 4 5 10
Output示例
5
二分解法:
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<bits/stdc++.h> using namespace std; const int maxn=50005; int N; int num[maxn],d[maxn],len; int Binary_Search(int now){ int l=1,r=len,m=(l+r)>>1; while(l<=r){ m=(l+r)>>1; if(now>d[m]&&now<=d[m+1])return m; if(now>d[m])l=m+1; else r=m-1; } return 0; } int LIS(){ int i,j=1; d[1]=num[1];len=1; for(i=2;i<=N;i++){ if(num[i]>d[len])j=++len; else j=Binary_Search(num[i])+1; d[j]=num[i]; } return len; } int main(){ scanf("%d",&N); for(int i=1;i<=N;i++)scanf("%d",&num[i]); printf("%d",LIS()); return 0; }利用stl函数解法:
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; int a[50005]; int f[50005]; int main() { int n,maxn; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); int ans=1; memset(f,0,sizeof(f));//创建一个新数组,用于存放最长上升序列 f[0]=a[0]; for(int i=1;i<n;i++) { int pos=lower_bound(f,f+ans,a[i])-f; f[pos]=a[i]; ans=max(ans,pos+1); } printf("%d",ans); return 0; }
相关文章推荐
- 1134 最长递增子序列
- 51Nod-1134 最长递增子序列
- 51Nod-1134-最长递增子序列
- 51nod--1134 最长递增子序列
- 51 nod 1134 最长递增子序列
- 51nod 1134 最长递增子序列
- 51nod 1134最长递增子序列
- 51nod 1134 最长递增子序列
- 【51NOD-0】1134 最长递增子序列
- 51nod 1134 最长递增子序列
- 51Nod 1134 最长递增子序列
- 51nod 1134 最长递增子序列
- 1134 最长递增子序列
- 51nod 1134 最长递增子序列
- 51nod 1134 最长递增子序列
- 51nod 1134 最长递增子序列
- 1134 最长递增子序列
- 51Nod 1134 最长递增子序列
- 【51nod】1134 最长递增子序列
- 51Nod 1134 最长递增子序列