您的位置:首页 > 其它

Bellovin(最长上升子序列)

2016-07-25 15:28 417 查看
题意:给定n个数,求到 i 为止的最长上升子序列的长度。

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5748

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int INF = 1e9 + 10;    //注意要超过元素可能的最大值
int a[100010],b[100010],c[100010];

int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
fill(c , c+n , INF);    将c中的元素全部填充最大值,注意c是有序的。所以一旦有超过最大值的值出现就会出错
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
c[0] = -1;
c[1] = a[0];
b[0] = 1;
for(int i=1; i<n; i++)
{
int l = lower_bound(c, c+n, a[i]) - c;
c[l] = a[i];
b[i] = l;
}
cout << b[0];
for(int j=1; j<n; j++)
{
cout << " " << b[j];
}
cout << endl;
}
return 0;
}


另:
lower_bround(a,a+n,k);即二分查找,返回元素k在数组a中的第一个位置。
upper_bround(a,a+n,k);也是二分查找,返回元素k在数组a中的最后一个位置。
upper_bround(a,a+n,k) - lower_bround(a,a+n,k);可以求出元素k在a中出现的次数。
注意:a为有序序列,时间复杂度为O(logn).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: