二分查找与二分答案-习题篇

(洛谷P1678)烦恼的高考志愿 

#include 
using namespace std;
int a[100005], b[100005];
#include 
#include 
int main() {
	int m, n;
	cin >> m >> n;
	for (int i = 1; i <= m; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) cin >> b[i];
	sort(a + 1, a + m + 1);
	long long sum = 0;
	for (int i = 1; i <= n; i++) {
		if (lower_bound(a + 1, a + m + 1, b[i]) - a == 1) {
			sum += a[1] - b[i];
		}
		else {
			int t = lower_bound(a + 1, a + m + 1, b[i]) - a;
			sum += min(abs(b[i] - a[t]), abs(b[i] - a[t - 1]));
		}
	}
	cout << sum;
	return 0;
}

(洛谷P2678)跳石头

贪心+二分

#include 
using namespace std;
int a[50005];
int l, n, m;
bool P(int x) {
	int i = 0;
	int del = 0;
	int now = 0;
	while (i < n + 1) {
		i++;
		if (a[i] - now < x) {
			del++;
		}
		else {
			now = a[i];
		}
	}
	if (del <= m) return 1;
	return 0;
}
int main() {
	cin >> l >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	a[0] = 0;
	a[n + 1] = l;
	int L = 1, R = l, mid,ans;
	while (L <= R) {
		mid = (L + R) / 2;
		if (P(mid)) {
			ans = mid; L = mid + 1;
		}
		else {
			R = mid - 1;
		}
	}
	cout << ans;
	return 0;
}

(洛谷P2440)木材加工

#include 
using namespace std;
int n, k;
int a[100005];
bool P(int m) {
	long long sum=0;
	for (int i = 1; i <= n; i++) {
		sum += a[i] / m;
	}
	return sum >= k;
}
int main() {
	cin >> n >> k;
	long long s=0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		s += a[i];
	}
	if (s < k) cout << 0;
	else {
		int L = 1, R = 1e8, mid, ans;
		while (L <= R) {
			mid = (L + R) / 2;
			if (P(mid)) {
				ans = mid; L = mid + 1;
			}
			else {
				R = mid - 1;
			}
		}
		cout << ans;
	}
	return 0;
}

(洛谷P3853)路标设置

#include 
using namespace std;
int l, n, k;
int a[100005];
bool judge(int x) {
	int now =a[1];
	int i = 1;
	int increase=0;
	while (i < n) {
		i++;
		if (a[i] - now <= x) {
			now = a[i];
		}
		else {
			int d = a[i] - now;
			if (d % x == 0) {
				increase += d / x - 1;
			}
			else {
				increase += d / x;
			}
			now = a[i];
		}
	}
	if (increase <= k) return 1;
	return 0;
}
int main() {
	cin >> l >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	int L = 1, R = l, mid,ans;
	while (L <= R) {
		mid = (L + R) / 2;
		if (judge(mid)) {
			ans = mid;
			R = mid - 1;
		}
		else {
			L = mid + 1;
		}
	}
	cout << ans;
	return 0;
}

(洛谷P1182)数列分段-Section II

#include 
using namespace std;
int a[100005];
int n, m;
bool judge(int x) {
	int time = 0;
	int i = 1;
	int sum = 0;
	int now = 1;
	for (int i = 1; i <= n; i++) {
		if (a[i] > x) return 0;
	}
	for (int i = 1; i <= n; i++) {
		sum += a[i];
		if (sum == x) {
			time++;
			sum = 0;
		}
		else if (sum > x) {
			time++;
			sum = 0;
			i -= 1;
		}
		else if (sum < x && i == n) {
			time++;
			break;
		}
	}
	return time <= m;
}
int main() {
	cin >> n >> m;
	int sum = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		sum += a[i];
	}
	int L = 1, R = sum, mid,ans;
	while (L <= R) {
		mid = (L + R) / 2;
		if (judge(mid)) {
			ans = mid;
			R = mid - 1;
		}
		else {
			L = mid + 1;
		}
	}
	cout << ans;
	return 0;
}

(洛谷P1163)银行贷款

#include 
using namespace std;
#include 
#include 
#define eps 1e-6
int A, P, M;
double p(double ans) {
	return (1.0 - pow(1.0 / (1 + ans), M)) / ans;
}
double f(double ans) {
	return A*p(ans) - P;
}
int main() {
	cin >> P >> A >> M;
	double L = 0, R = 3, mid, ans;
	while (L <= R) {
		mid = (L + R) / 2;
		if (fabs(f(mid)) <= eps) {
			ans = mid;
			break;
		}
		else if (f(mid) - eps > 0) {
			L = mid;
		}
		else if(f(mid) + eps < 0){
			R = mid;
		}
	}
	printf("%.1f", ans * 100);
	return 0;
}

 

你可能感兴趣的:(数据结构与算法,算法,c++,数据结构)