POJ3497 Assemble(二分)

181 篇文章 0 订阅
73 篇文章 0 订阅


Link:http://poj.org/problem?id=3497


Assemble
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3623 Accepted: 1144

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.

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

Source


AC code:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
#define LL long long
#define INF 0xfffffff
#define PI acos(-1)
#define EPS 1e-6
using namespace std;

struct node{
	int price;
	int quality;
};

int n,b,num;
map<string,int>m;
vector<node>v[1010];

bool buy(int m,int b)
{
	int cnt=0,i,j,mon,minp;
	mon=b;
	for(i=1;i<=num;i++)
	{
		minp=INF;
		for(j=0;j<v[i].size();j++)
		{
			if(v[i][j].quality>=m && v[i][j].price<=minp)
			{
				minp=v[i][j].price;
			}
		}
		if(minp==INF)
			return false;
		else
		{
			mon-=minp;
			if(mon<0)
				return false;
		}	
	}
	return true;
}

int Bsearch(int l,int h)
{
	int m,res;
	res=0;
	while(l<=h)
	{
		m=(l+h)>>1;
		if(buy(m,b))
		{
			res=m;
			l=m+1;
		}
		else
		{
			h=m-1;
		}
	}
	return res;
}

int main()
{
//	freopen("in.txt","r",stdin);
	int t,i,p,q,low,high,ans;
	char typ[111],nam[111];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&b);
		m.clear();
		num=0;
		low=0;
		high=0;
		for(i=0;i<n;i++)
		{
			scanf("%s%s%d%d",&typ,&nam,&p,&q);
			high=max(high,q);
			if(m.find(typ)==m.end())
			{
				m[typ]=++num;
				v[m[typ]].clear();
			}
			node data;
			data.price=p;
			data.quality=q;
			v[m[typ]].push_back(data);
		}
		ans=Bsearch(low,high);
		printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值