【总结】【线段树】2016.1.24CXB
来源:互联网 发布:百度关键词挖掘软件 编辑:程序博客网 时间:2024/06/09 02:58
O(nlogn)的算法关键是它建立了一个数组temp[],temp[i]表示长度为i的不下降序列中结尾元素的最小值,用top表示数组目前的长度,算法完成后top的值即为最长不下降子序列的长度。
设当前的以求出的长度为top,则判断num[i]和temp[top]:
1.如果num[i]>=temp[top],即num[i]大于长度为top的序列中的最后一个元素,这样就可以使序列的长度增加1,即top++,然后现在的temp[top]=num[i];
2.如果num[i]<temp[top],那么就在temp[1]...temp[top]中找到最大的j,使得temp[j]<num[i],然后因为temp[j]<num[i],所以num[i]大于长度为j的序列的最后一个元素,那么就可以更新长度为j+1的序列的最后一个元素,即temp[j+1]=num[i]。摘自:http://www.cnblogs.com/wuyiqi/archive/2011/11/20/2256537.html
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int oo=100000007;int n;int num[1000001];int lon[1000001];int ans;int main(){freopen("up_longest.in","r",stdin);freopen("up_longest.out","w",stdout);scanf("%d",&n);for(int i=0;i<n;++i)scanf("%d",&num[i]);for(int i=1;i<=n;++i)lon[i]=oo;lon[0]=-oo;ans=0;for(int i=0;i<n;++i){int tmp=upper_bound(lon,lon+n,num[i])-lon;if(lon[tmp]==oo)lon[tmp]=num[i];elseif(lon[tmp]>num[i])lon[tmp]=num[i];if(tmp>ans)ans=tmp;}printf("%d\n",ans);return 0;}
#include <iostream>#include <algorithm>//必须包含的头文件using namespace std;int main()
{ int point[10] = {1,3,7,7,9}; int tmp = upper_bound(point, point + 5, 7) - point;//按从小到大,7最多能插入数组point的哪个位置,即最后一个小于等于它的数的位置+1 printf("%d\n",tmp); tmp = lower_bound(point, point + 5, 7) - point;////按从小到大,7最少能插入数组point的哪个位置,即第一个大于等于它的数的位置-1 printf("%d\n",tmp); return 0;}
- 【总结】【线段树】2016.1.24CXB
- 【总结】2015.12.20CXB
- 20150515cxb
- 线段树总结
- 简单线段树总结
- 线段树总结
- 线段树总结
- 线段树总结
- 线段树总结
- 线段树总结
- 线段树总结
- 线段树总结
- 线段树总结
- 线段树总结
- 线段树总结
- 线段树 总结
- 线段树总结
- 线段树最后总结
- MIPS CP0 Hazards问题--从Release1到Release2
- 【数据结构】树状数组
- MIPS架构培训之MMU原理
- 【数据结构】线段树
- 异常捕捉与反汇编分析
- 【总结】【线段树】2016.1.24CXB
- 模拟指令断点
- 【转载】【线段树】最长不上升序列
- Nand ECC学习
- 【选拔题】2009南海集训队初中选拔题一
- 花样滑冰教程视频
- 2016佛山市GDOI选拔赛(am)
- 一个成功的Git分支模型
- 【USACO】JAN.BRONZE