NBUT-2014暑期集训专题练习1 -> 二分法 N - N

本文讨论了如何通过购买并组装独立组件来创建理想的编程竞赛计算机,以最大化最低质量组件的同时不超出预算。文章提供了算法解决方案,帮助读者根据可用组件和预算选择最佳配置。

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

Description

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 ≤  1 000 , the number of available components and 1 ≤ b ≤  1 000 000 000 , 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 ≤  1 000 000 ) which represents the price of the component and quality is an integer (0 ≤ quality ≤  1 000 000 000 ) 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.

Output

Per testcase:

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

二分题,既要最大化最小值,也要让价格尽量小,那么在二分答案的时候,选择的当然是满足当前mid的,价格又是最小的组件了,一开始自己用个struct来保存,最后超时了,然后换成vector 的做法, 就能过了。547ms.....

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

vector<int>price[1010];
vector<int>quality[1010];
int main()
{

int t,n,buget;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d%d",&n,&buget);
int i,j;
map<string,int>zujian;
zujian.clear();
char str1[22],str2[22];
int cnt=1,a,b;
int maxn=-1,mins=0x3f3f3f3f;
for(i=0;i<n;i++)
{
scanf("%s%s%d%d",str1,str2,&a,&b);
if(zujian[str1]==0)//新组件
{
zujian[str1]=cnt;
price[cnt].clear();
quality[cnt].clear();
price[cnt].push_back(a);
quality[cnt].push_back(b);
if(maxn<b)
maxn=b;
if(mins>b)
mins=b;
cnt++;
}
else
{
price[zujian[str1]].push_back(a);
quality[zujian[str1]].push_back(b);
if(maxn<b)
maxn=b;
if(mins>b)
mins=b;
}
}
int l=mins,r=maxn,mid;
bool flag;
while(l<r)
{
mid=(l+r+1)>>1;
int all;
i=1;
int money=0;
bool flag=false;
while(i<cnt)
{
all=0x3f3f3f3f;
//printf("%d\n",cen[i]);
for(j=0;j<quality[i].size();j++)
if(quality[i][j]>=mid)
all=min(all,price[i][j]);
// printf("%d\n",all);
if(all==0x3f3f3f3f)
{
flag=true;
break;
}
money+=all;
i++;
}
// printf("%d %d %d %d %d %d\n",l,r,mid,money,buget,flag);
if(flag || money>buget)
r=mid-1;
else
l=mid;
}
printf("%d\n",l);
}
}
return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值