您的位置:首页 > 其它

【最长上升子序列】51 nod 最长单增子序列

2017-08-07 21:00 141 查看
输入

第1行:1个数N,N为序列的长度(2 <= N <= 50000)

第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= S[i] <= 10^9)

输出

输出最长递增子序列的长度。

输入示例

8

5

1

6

8

2

4

5

10

输出示例

5

思路:

平时的方法,O(n * n)会超时,得用二分,我用的是STL的lower_bound



#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int dp[50055];
int main()
{

int n, num;
while(~scanf("%d", &n))
{
memset(dp, inf, sizeof(dp));//初始化为无穷大,
//因为dp数组是从小到大存数的。
for(int i = 0; i < n; i++)
{
scanf("%d", &num);
*lower_bound(dp, dp + n, num) = num;//找到大于等于num的第一个位置
}
printf("%d\n", lower_bound(dp, dp + n, inf) - dp);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: