基准时间限制: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<stdio.h>
#include<algorithm>
using namespace std;
int dp[50050];
int main()
{
int i,n,len=1,temp;
scanf("%d",&n);
scanf("%d",&dp[1]);
for(i=2;i<=n;i++)
{
scanf("%d",&temp);
if(temp>dp[len])
{
dp[++len]=temp;
}
else
{
int j=lower_bound(dp+1,dp+len+1,temp)-dp;
dp[j]=temp;
}
}
printf("%d",len);
return 0;
}