POJ3497 Assemble(二分)

来源:互联网 发布:BFGS算法程序 编辑:程序博客网 时间:2024/06/16 10:35


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


Assemble
Time Limit: 1000MS Memory Limit: 65536KTotal 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

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

Source

Northwestern Europe 2007

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-6using 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;}