C++中的lower_bound和upper_bound()怎么在实际里应用?
  • 板块灌水区
  • 楼主ALANYUEHNV
  • 当前回复6
  • 已保存回复6
  • 发布时间2022/4/26 17:02
  • 上次更新2023/10/28 02:51:47
查看原帖
C++中的lower_bound和upper_bound()怎么在实际里应用?
688779
ALANYUEHNV楼主2022/4/26 17:02

题目

#include <bits/stdc++.h>
const int maxn=500005;
using namespace std;
int l, r, n, m, ans, mid, d;
int a[maxn];
bool pd(int x) {
	int tot = 0;
	int i = 0;
	int now = 0;
	while(i < n + 1) {
		i++;
		if(a[i] - a[now] < x) {
			tot++;
		} else {
			now = i;
		}
	}
	if(tot > m) {
		return false;
	} else {
		return true;
	}
}
int main() {
	cin >> d >> n >> m;
	for(int i = 1; i <= n; i++) cin >> a[i];
	a[n + 1] = d;
	l = 1, r = d;
	while(l <= r) {
		mid = (l + r) / 2;
		if(pd(mid)==true) {
			ans = mid;
			l = mid + 1;
		} else r = mid - 1;
	}
	cout << ans << endl;
	return 0;
}

标题的两个函数如何应用在其中?

2022/4/26 17:02
加载中...