lower_bound函数和upper_bound函数的用法

都需要头文件:

#include<algorithm>
using namespace std;//algorithm.h需要

lower_bound函数从已经排好序的序列a中利用二分搜索找出指向满足a[i]>=k的a[i]的最小的指针

lower_bound(a,a+n,k)

upper_bound函数从已经排好序的序列a中利用二分搜索找出指向满足a[i]>k的a[i]的最小的指针

upper_bound(a,a+n,k)

可以用如下代码求出长度为n的有序数组a中的k的个数

num=upper_bound(a,a+n,k)-lower_bound(a,a+n,k);

将有序数组a中k所对应位置的元素改为k;

*lower_bound(a,a+n,k)=k;

简单例子:
从单调不下降序列a中利用二分搜索找出满足a[i]>=k的a[i]的最小的i
从单调不下降序列a中利用二分搜索找出满足a[i]>=k的a[i]的最小的i

#include<cstdio>
#include<algorithm>
using namespace std;
int a[100000];
int main(){
	int n,k; 
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	printf("%d\n",lower_bound(a,a+n,k)-a);
	printf("%d\n",upper_bound(a,a+n,k)-a);
	return 0;
}
/*
输入 
5 3
2 3 3 5 6 
输出
1
3 
*/ 

题目妙用:
求数组a的LIS(最长上升子序列)。
O(nlogn)复杂度方法:

//长度相同情况下的末尾元素中的最小值 
//dp[i]长度为i+1的上升子序列中末尾元素的最小值(不存在的话就是INF) 
#include<cstdio>
#include<algorithm>
#define MAX_N 1005
#define INF 1000010
using namespace std;
int dp[MAX_N];
int n,a[MAX_N];
void solve(){
	fill(dp,dp+n,INF);
	for(int i=0;i<n;i++) //依次输入n个数
		*lower_bound(dp,dp+n,a[i])=a[i];
	printf("%d\n",lower_bound(dp,dp+n,INF)-dp);
} 
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	solve();
	return 0;
}

或者简写为:

#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	int dp[1005],n,ai;
	scanf("%d",&n);
	fill(dp,dp+n,1000010);
	for(int i=0;i<n;i++){
		scanf("%d",&ai);
		*lower_bound(dp,dp+n,ai)=ai;
	 } 
	printf("%d\n",lower_bound(dp,dp+n,1000010)-dp);
	return 0;
}
/*
输入 
5 
4 2 3 1 5
输出
3 
*/ 
展开阅读全文

lower_boundupper_bound用法

05-31

在msdn上面查找lower_bound:rnReturns an iterator to the first element in a hash_map with a key value that is equal to or greater than that of a specified key. rn使用它的例子rn[code=C/C++]rn// hash_map_lower_bound.cpprn// compile with: /EHscrn#define _DEFINE_DEPRECATED_HASH_CLASSES 0rn#include rn#include rnrnint main( )rnrn using namespace std;rn using namespace stdext;rn hash_map hm1;rn hash_map :: const_iterator hm1_AcIter, hm1_RcIter;rn typedef pair Int_Pair;rnrn hm1.insert ( Int_Pair ( 1, 10 ) );rn //改动处rn //hm1.insert ( Int_Pair ( 2, 20 ) );rn hm1.insert ( Int_Pair ( 3, 30 ) );rnrn hm1_RcIter = hm1.lower_bound( 2 );rn cout << "The first element of hash_map hm1 with a key of 2 is: "rn << hm1_RcIter -> second << "." << endl;rnrn // If no match is found for the key, end( ) is returnedrn hm1_RcIter = hm1. lower_bound ( 4 );rnrn if ( hm1_RcIter == hm1.end( ) )rn cout << "The hash_map hm1 doesn't have an element "rn << "with a key of 4." << endl;rn elsern cout << "The element of hash_map hm1 with a key of 4 is: "rn << hm1_RcIter -> second << "." << endl;rnrn // An element at a specific location in the hash_map can be rn // found using a dereferenced iterator addressing the locationrn hm1_AcIter = hm1.end( );rn hm1_AcIter--;rn hm1_RcIter = hm1. lower_bound ( hm1_AcIter -> first );rn cout << "The element of hm1 with a key matching "rn << "that of the last element is: "rn << hm1_RcIter -> second << "." << endl;rnrnrn[/code]rnrnhm1.insert ( Int_Pair ( 2, 20 ) );rn这一行注释掉了~这个lower_bound不是应该返回3吗?怎么是个end啊。。。 论坛

没有更多推荐了,返回首页