HDU - 1257 最小拦截系统(lower_bound)

传送门

可以用lower_bound优化一下,lower_bound的返回值是指向第一个大于等于value的地址,如果序列里面的数全部大于value则返回指针最后一个元素的下一个位置,比如说有一个数组a[5]={1,2,3,4,5},我们查找的值value=6,在数组里面找不到比value大的数,所以lower_bound返回一个指针指向a[4]的下一个,相当于a[5]

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define father  l , r , root
#define lowbit(x) ( x & ( - x ) )
#define mod 1000000007
using namespace std;
const int maxn = 5e5+10;
const int inf = 0x3f3f3f3f;
int a[maxn];
int main()
{
    
    
	int n;
    while(~scanf("%d",&n)){
    
    
    	int cnt=0;
    	int m;
    	memset(a,0,sizeof(a));
    	for(int i=0;i<n;i++){
    
    
    		scanf("%d",&m);
    		bool flag=false;
    		int x=lower_bound(a,a+cnt,m)-a;
    		if(x==cnt){
    
    
    			a[cnt++]=m;
			}else{
    
    
				a[x]=m;
			}
		}
		printf("%d\n",cnt); 
	}
    return 0;
}
/*
6 8 7 3 6 2 4 
*/

猜你喜欢

转载自blog.csdn.net/qq_43715171/article/details/97794698