The Delivery Dilemma

链接 : http://codeforces.com/problemset/problem/1443/C

标签 : binary search greedy sorting *1400

二分答案

AC代码
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi firsqt
#define se second
#define inf 0x3f3f3f3f
const int N = 2e5 + 7;
ll a[N], b[N];
int n;
 
bool check(ll x) {
	ll sum = 0, ma = 0;
	for (int i = 0; i < n; ++i) {
		if (a[i] <= x) ma = max(ma, a[i]);
		else sum += b[i];
	}
	ma = max(ma, sum);
	return ma <= x;
}
 
int main() {	
	IO;
	int _;
	cin >> _;
	while (_--) {
		cin >> n;
		for (int i = 0; i < n; ++i) cin >> a[i];
		for (int i = 0; i < n; ++i) cin >> b[i];
		ll l = 1, r = 1e18;			
		while (l < r) {
			ll mid = l + r >> 1;
			if (check(mid)) r = mid;
			else l = mid + 1;
		}
		cout << l << endl;
	}
	return 0;
}

`

posted @ 2020-11-04 22:55  phr2000  阅读(98)  评论(0编辑  收藏  举报