【模板】二分

基本通用的模板:(同于模板1,不同的写法)

int erf(int le, int ri) {
	while (le <= ri){
		int mid = (le + ri) >>1;
		if (check(mid))
			le = mid + 1;
		else
			ri = mid - 1;
		}
	return le - 1;
}

模板1:满足条件的最大值(最大的小值)

int erf(int le, int ri) {//求满足条件的最大值
	while (le + 1 <ri) {//防止死循环
		int mid = le + ri >> 1;// '+'优先级大于'>>'
		if (check(mid))//check()函数:mid满足条件
			le = mid;
		else
			ri = mid;
	}
	return le;
}

模板2:满足条件的最小值(最小的大值)

int erf(int le, int ri) {//求满足条件的最小值
	while (le + 1 <ri) {//防止死循环
		int mid = le + ri >> 1;// '+'优先级大于'>>'
		if (check(mid))//check()函数:mid满足条件
			ri = mid;
		else
			le = mid;
	}
	return ri;
}

 模板3:满足条件的只有一个值

int erf(int le, int ri) {
	while (le + 1 <ri) {
		int mid = le + ri >> 1;
		if (check(a[mid]))
			return mid;
		else if(a[mid]<5)
			le = mid;
		else
			ri = mid;
	}
	return 0;
}

  

 

模板2和模板1的区别:

  模板1满足条件,left变,向右找,所以是找最大的满足条件的

  模板2满足条件,right变,向左找,所以是找最小的满足条件的

 

通用模板例题:(属于模板1类型)

Aggressive cows:二分+贪心

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). 

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Input

* Line 1: Two space-separated integers: N and C 

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

* Line 1: One integer: the largest minimum distance

Sample Input

5 3
1
2
8
4
9

Sample Output

3

Hint

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3. 

Huge input data,scanf is recommended.

题目大意: n个牛棚安排c个牛,求这c头牛两头之间的在最优的方案下,最大的距离

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int n, c, a[100010];

int read() {//用cin会超时
	int noo1 = 0, noo2 = 1;
	char ch = getchar();
	while (ch<'0' || ch>'9') { if (ch == '-')noo2 = -1; ch = getchar(); }
	while (ch >= '0'&&ch <= '9') noo1 = noo1 * 10 + ch - '0', ch = getchar();
	return noo1*noo2;
}

int check(int x) {
	int cnt = 1, ans = a[0];//ans记录上一个安排的牛棚
	for (int i = 1; i < n; i++) {
		if (a[i] - ans >= x) {
			cnt++;
			ans = a[i];
		}
		if (cnt >= c)//如果满足条件,能安排下
			return 1;
	}
	return 0;//不能安排下
}

int erf(int le, int ri) {//基本通用模板
	while (le <= ri) {
		int mid = (le + ri) >> 1;
		if (check(mid))
			le = mid + 1;
		else
			ri = mid - 1;
	}
	return le - 1;
}

int main() {
	cin >> n >> c;
	for (int i = 0; i < n; i++)
		a[i] = read();

	sort(a, a + n);//二分前提:单调性

	cout << erf(0, a[n - 1] - a[0]) << "
";
	return 0;
}

  

模板1小例题(假象):

求1~9中小于5的最大值

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool check(int x) {
	if (x < 5)return 1;//假定在1~9中,大于5的满足条件
	else return 0;
}
int erf(int le, int ri) {
	while (le + 1 <ri) {
		int mid = le + ri >> 1;
		if (check(mid))
			le = mid;
		else
			ri = mid;
	}
	return le;
}
int main() {
	cout << erf(1, 9) << "
";
	return 0;
}
//结果输出4

 

模板2小例题(假象):

求1~9中大于5的最小值

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool check(int x) {
	if (x > 5)return 1;//假定在1~9中,大于5的满足条件
	else return 0;
}
int erf(int le, int ri) {
	while (le + 1 <ri) {
		int mid = le + ri >> 1;
		if (check(mid))
			ri = mid;
		else
			le = mid;
	}
	return ri;
}
int main() {
	cout << erf(1, 9) << "
";
	return 0;
}
//结果输出6

  

模板3小例题(假象):

求1~9中等于5的数

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int a[20];

bool check(int x) {
	if (x == 5)return 1;//在1~9中,等于5的满足条件
	else return 0;
}
int erf(int le, int ri) {
	while (le + 1 <ri) {
		int mid = le + ri >> 1;
		if (check(a[mid]))
			return mid;
		else if(a[mid]<5)
			le = mid;
		else
			ri = mid;
	}
	return 0;
}
int main() {
	for (int i = 0; i < 10; i++)
		a[i] = i;
	cout << erf(a[0], a[9]) << "
";
	return 0;
}
//结果输出5

  

 
原文地址:https://www.cnblogs.com/52dxer/p/10389166.html