Assemble

来源:互联网 发布:java作业题 编辑:程序博客网 时间:2024/04/29 09:11

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 ≤ 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

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

Sample Output

9
这道题是求解诸多最小值中的最大值问题。我们用二分法来求解。方法是:首先选取一个数x(一般是中位数),把整个区间分为【a,x-1】和【x,b】假定它是最大值,如果比他大的数满足条件,那么它就不是最大值,最大值将位于【x,b】上。然后再把区间一分为二,继续求解。除了用二分法求解这道题中的最值问题,我还从这道题中学到了不少东西。比如,这道题的输入数据是字符和整型,且一种类型的配件最好集中在一起存放,如果用结构体来存放显然不是最好的方法,所以我们用向量容器vector来存放。再比如,map的使用,它提供了字符型的键,实现了按配件类型进行索引。代码如下:
#include<iostream>#include<cstdio>#include<vector>#include<map>using namespace std;int ans,m,b;map<string,int> Type;int ID(string s){if(!Type.count(s)) Type[s]=ans++;return Type[s];}struct component{int price;int quality;};vector<component> comp[1005]; bool ok(int mq){int sum=0;for(int i=0;i<ans;i++){int cheap=b;for(int j=0;j<(comp[i].size());j++){if(comp[i][j].quality>=mq) cheap=min(comp[i][j].price,cheap);}if(cheap==b) return false;sum+=cheap;}if(sum>b) return false;else return true;}int main(){int T;cin>>T;while(T--){int n,maxq=0;ans=0;scanf("%d%d",&n,&b);Type.clear();        //千万不要忘记!for(int i=0;i<n;i++)comp[i].clear();for(int i=0;i<n;i++){char type[22],name[22];int p,q;scanf("%s%s%d%d",type,name,&p,&q);maxq=max(maxq,q);comp[ID(type)].push_back((component){p,q});}int l=0,r=maxq;while(l<r){m=l+(r-l+1)/2;if(ok(m)) l=m;else r=m-1;}printf("%d\n",l);}return 0;}
(写完程序之后,我测试了好几次都未通过,最后发现是未将comp和Type内的数据清除)
这里我要说一下vector。如果我们定义vector<component> comp,那么我们是定义了一个vector,vector是没有长度限制的,你可以尽情地加;但是如果我们定义vector<component> conp[maxn],那就是定义了一堆vector,那我们就需要声明一共有多少个vector,而每一个vector长度是没有限制的。

0 0