1134 最长递增子序列
基准时间限制: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
这题可用二分查找,可用stl函数库中的lower_bound函数或者upper_bound函数;
lower_bound函数,简单的理解就是(现在有一个数组b,里边有1,3,4;a数组中有个2,利用lower_bound函数后,即把2插进了数组b,此时b数组为1,2,4);upper_bound函数和lower_bound
函数的区别在于,b数组有重复数时,前者替换的是重复数的最后一个,而后者替换的则是第一个。
#include <iostream> #include <algorithm> using namespace std; int b[100001],a[100001]; int main() { int n,x=1,i,k; cin>>n; for(i=1;i<=n;i++)cin>>a[i]; b[x]=a[1]; for(i=2;i<=n;i++) { if(a[i]>b[x]) b[++x]=a[i]; else { k=lower_bound(b+1,b+x,a[i])-b;//k=lower_bound(b,b+x,a[i])-b;// k=upper_bound(b+1,b+x,a[i])-b; b[k]=a[i]; } } cout<<x<<endl; return 0; }

浙公网安备 33010602011771号