UVALIVE 3971<最大化最小值>

本文探讨如何在预算约束下最大化计算机中最弱组件的质量,通过分析不同组件的价格与质量,实现理想计算机的构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <ctime>
using namespace std;
//ios::sync_with_stdio(false);
struct node
{
	int p,q;
	
};
struct nu
{
	int id,q,p;
	bool operator < (const nu& t)const
	{
		return q > t.q;
	}
};

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n,b;
		cin >> n >> b;
		map<string,int> ma;
		int cnt = 0;
		vector<node> vec[1005];
		
	
		for(int i = 0;i < n;i++)
		{
			char ty[30],na[30];
			node a;

			cin >> ty >> na >> a.p >>  a.q;
			if(ma.count(ty) == 0)	
			{
				vec[cnt].push_back(a);
				ma[ty] = cnt;
				cnt++;
			}
			else 
			{
				vec[ma[ty]].push_back(a);
			}
		}
		int sum = 0;
		priority_queue<nu> que;
		int ans = INT_MAX;
		
		for(int i = 0 ; i < cnt;i++)
		{
			int m =  vec[i].size();
			for(int j = 0;j < m;j++)
			{
				for(int k = j+1; k < m;k++)
				{
					if(vec[i][j].p > vec[i][k].p)
						swap(vec[i][j],vec[i][k]);
				}
			}
			sum += vec[i][0].p;
			nu a;
			a.id = i;
			a.q = vec[i][0].q;
			a.p = vec[i][0].p;
			if(a.q < ans)
				ans = a.q;
			que.push(a);
		}
		while(1)
		{
			nu tm = que.top();
			que.pop();
			ans = tm.q;
			int en = vec[tm.id].size();
			int i;
			for(i = 0; i < en; i++)
			{
				if(vec[tm.id][i].q > tm.q)
				{
					break;
				}
			}
			if(i < en)
			{
				nu a;
				a.id = tm.id;
				a.q = vec[tm.id][i].q;
				a.p = vec[tm.id][i].p;
				que.push(a);
				sum -= tm.p;
				sum += a.p;
				if(sum > b)
					break;
			}
			else
			{
				break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
最大化最小值的思路是从先找到一个能用的方案,然后找到一个能替代最小值的的元素替代掉最小值,如果替代之后的方案仍然符合要求那么这个最小值就继续按照上述思路更新最小值

该题中首先采用一个在每一个种类的配件中选一个最小的价值的配件,如果这个方案都不符合要求的话那么就无解了,然后从品质最小的配件的种类中挑选出一个品质比他要高的元件,因为价格是按照升序排列的所以选出来的如果不可以的话说明就不能更新最小值了,所以这是一个最优的方案啦。乳沟还是符合方案 的话就一直按照这个歌思路进行更新最小值。

UVALive - 3971
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 Status

Description

Download as PDF

Problem A - Assemble

Time limit: 2 seconds

Recently your team noticed that the computer you use to practice for programming contests is not good enough anymore. Therefore, you decide to buy a new computer.

To make the ideal computer for your needs, you decide to buy separate components and assemble the computer yourself. You need to buy exactly one of each type of component.

The problem is which components to buy. As you all know, the quality of a computer is equal to the quality of its weakest component. Therefore, you want to maximize the quality of the component with the lowest quality, while not exceeding your budget.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

  • One line with two integers: 1 ≤ n ≤ 1000, the number of available components and 1 ≤ b ≤ 1000000000, your budget.
  • n lines in the following format: ``type name price quality'', where type is a string with the type of the component, name is a string with the unique name of the component, price is an integer (0 ≤ price< 1000000) which represents the price of the component and quality is an integer (0 ≤ quality ≤ 1000000000) which represents the quality of the component (higher is better). The strings contain only letters, digits and underscores and have a maximal length of 20 characters.

It will always possible to construct a computer with your budget.

Output

Per testcase:

  • One line with one integer: the maximal possible quality.

Sample Input

1
18 800
processor 3500_MHz 66 5
processor 4200_MHz 103 7
processor 5000_MHz 156 9
processor 6000_MHz 219 12
memory 1_GB 35 3
memory 2_GB 88 6
memory 4_GB 170 12
mainbord all_onboard 52 10
harddisk 250_GB 54 10
harddisk 500_FB 99 12
casing midi 36 10
monitor 17_inch 157 5
monitor 19_inch 175 7
monitor 20_inch 210 9
monitor 22_inch 293 12
mouse cordless_optical 18 12
mouse microsoft 30 9
keyboard office 4 10

Sample Output

9


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值