Bellovin(最长上升子序列)
2016-07-25 15:28
417 查看
题意:给定n个数,求到 i 为止的最长上升子序列的长度。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5748
代码:
链接: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).
相关文章推荐
- 关于文字环绕图片的问题
- [编写高质量iOS代码的52个有效方法](三)消息和运行期
- libsvm使用教程
- 前后端分离项目实践分析
- 带有两层和三层列表的ExpandableListView
- GoEasy推送实时消息到web端
- 65.Which two statements are true about checkpointing? (Choose two.)
- 详解Swift基本运算符
- javascript的继承模式的总结
- UILabel设置行间距之后的自适应高度
- 石子合并(区间型动规)
- Java Programming Test Question 3
- 【转】机器学习资料汇总
- Android仿IOS加载动画
- javaWeb学习(1)——servlet生命周期
- iOS本地推送消息
- Git 中的修改与提交比较(05)
- translucent影响半透明的状态
- Bootstrap tokenfield 使用指南
- 旋转字符及其判断