LA3971 - Assemble

照着思路敲了一下二分答案,发现很神奇。M = L + (R - L + 1) / 2 为什么能一定逼近答案呢,有没有可能这个答案根本不存在,只是在两个答案之间。

#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#define MAXN 100
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
using std::min;
using std::max;
map<string, int> id;
//
int cnt;
int n, b;
int ID(string s) {
	if (!id.count(s)) id[s] = cnt ++;
	return id[s];
}
struct P {
	int price, quality;
};
vector<P> comp[1010];
bool check(int q) {
	int sum = 0;
	for (int i = 0; i < cnt; ++ i) {
		int Min = b + 1;
		for (int j = 0; j < comp[i].size(); ++ j) {
			if (comp[i][j].quality >= q)
				Min = min(comp[i][j].price, Min);
		}
		sum += Min;
		if (sum > b) return false;
	}
	return true;
}
int main() {
	//freopen("input.in", "r", stdin);
	//freopen("1.out", "w", stdout);
	int t;
	cin >> t;
	while (t --) {
		cin >> n >> b;
		for (int i = 0; i < n; ++ i) comp[i].clear(); id.clear();
		cnt = 0;
		int Max = 0;
		for (int i = 0; i < n; ++ i) {
			string type, name;
			int p, q;
			cin >> type >> name >> p >> q;
			Max = max(Max, q);
			comp[ID(type)].push_back((P){p, q});
		}
		int L = 0, R = Max;
		while (L < R) {
			//cout << L << " " << R << endl;
			//puts("ok");
			//int M = (L + R + 1) >> 1;
			int M = L + (R - L + 1) / 2;
			if (check(M)) L = M;
			else R = M - 1;
		}
		cout << L << endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值