您的位置:首页 > 其它

hdu 2333 Assemble 二分

2016-07-05 22:23 483 查看
题意:一个人拿着一定的钱数要配置一台电脑,现在给出各个部件,每个部件有多个产品,只需要选用一个产品。

每个产品都有话费和质量(quality)。电脑的质量取决于其中质量最低部件。问能够组成电脑的最大质量。

解法:二分。我一开始不是这样想的。别人告诉我二分来写的。我一开始误入歧途,认为是需要优化的分组背包。

但数据实在太多太强,不是TLE就是MLE。

现在想想当时还是太草率了,首先比赛时做一个题要选择最省时间和精力的方法,这个题显然可以将电脑质量分为两段,大于等于某个值的都是不能实现的,小于等于某个值的都是可以的。判断一个质量是否可以作为ans,只需要贪心搞搞即可。明显的二分......这题一点都不难。


Assemble

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 628    Accepted Submission(s): 230


Problem 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

Northwestern Europe 2007

 

Recommend

zty   |   We have carefully selected several similar problems for you:  1551 2446 2298 1905 2648 

 

Statistic | Submit | Discuss | Note

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 1000 ;

int n,V,EQU,cost[maxn+5],qua[maxn+5];
map<string ,int >mp;
int getId(string s)
{
if(!mp.count(s))
{
int k=mp.size();
mp[s]=k;
return k;
}
return mp[s];

}
vector<int >G[maxn+10];
void init()
{
mp.clear();
EQU=0;
}

bool can(int mid)
{
// cout<<"mid"<<mid<<endl;
int kind=mp.size();
int tot=0;
for0(i,kind)
{
int mini=INF;
// cout<<i<<":"<<endl;
for0(j,G[i].size())
{
int k=G[i][j];
// cout<<k<<" ";
if(qua[k]<mid) continue;
mini=min(mini,cost[k]);
}
if(mini==INF) return false;
tot+=mini;
if(tot>V) return false;
// cout<<endl;
}

return true;

}

void clear()
{
int k=mp.size();
for0(i,k)
{
G[i].clear();
}
}
int solve(int le,int ri)
{
while(le<=ri)
{
int mid=(le+ri)>>1;
if(!can(mid)) ri=mid-1;
else le=mid+1;
}
clear();
return ri;
}
int main()
{
int T;string s;
cin>>T;
while(T--)
{
init();
cin>>n>>V;
for1(i,n)
{
cin>>s;
int id=getId(s);
cin>>s>>cost[i]>>qua[i];
G[id].push_back(i);
EQU=max(EQU,qua[i]);
}
// cout<<mp.size()<<endl;
printf("%d\n",solve(0,EQU));
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: