二分有什么返例可以说他错了吗??T.T想不到阿
查看原帖
二分有什么返例可以说他错了吗??T.T想不到阿
825331
Jiaq楼主2023/4/24 22:09
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int s[5], len, total;
int a[25];
int sum, l, r;

bool check(int x) {
	int res = 0;
	for (int i = len; i >= 1; i -- ) {
		if (res + a[i] <= x) {
			res += a[i];
		}
	}
	return max(res, total - res) <= x;
}
//如果找到一个x时间使得存在一组时间的最大值不超过他,则往更小的时间去找
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	for (int i = 1; i <= 4; i ++ ) cin >> s[i];
	
	for (int i = 1; i <= 4; i ++ ) {
		total = 0, len = s[i];
		for (int j = 1; j <= s[i]; j ++ ) {
			cin >> a[j];
			total += a[j];
		}
		
		sort(a + 1, a + s[i] + 1);
		l = total / 2, r = total;
		while (l < r) {
			int mid = l + r >> 1;
			if (check(mid)) r = mid;
			else l = mid + 1;
		}
		//cout << "l  " << l << endl;
		sum += l;
	}
	
	cout << sum << endl;
	
	
	
	
    return 0; //圆满结束
	

}

2023/4/24 22:09
加载中...