解题报告:HDU_2333 Assemble 二分

来源:互联网 发布:人工智能电影精彩影评 编辑:程序博客网 时间:2024/06/05 02:31

Assemble

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 654    Accepted Submission(s): 237


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

Recommend
zty
 

Statistic | Submit | Discuss | Note

题意:
挑电脑配件,已知n个配件类别,价格,性能,问给定价格的最大化的电脑性能。
要求每类配件必须挑一个,电脑的性能为所挑配件里的最低性能。

思路:
二分最低性能,每个配件选符合最低性能的最低价格,如果价格不超过总钱数则满足要求。
将字符串映射成对应的编号:
如果用的map<string,int>,在G++会WA,要交C++。
也可以用数组,但代码量会大一点。

代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<queue>#include<map>#include<iostream>using namespace std;map<string,int>M;struct obj{    int pr;    int k;    obj(){}    obj(int a,int b){        pr = a;k =b;    }}A[1005][1005];int cnt[1005],m;int n,sum,price,k;inline bool cmp(const obj& a,const obj& b){    if(a.k==b.k){        return a.pr<b.pr;    }return a.k>b.k;}bool judge(int x){    int res = sum;    for(int i=1;i<=m;i++){//printf("i=%d\n",i);        int id = -1 , p = res;        for(int j=0;j<cnt[i];j++){            if(A[i][j].k>=x){                if(A[i][j].pr<=p){                    id = j;                    p = A[i][j].pr;                }            }        }if(id==-1){            return false ;        }else {            res -= p;        }    }    return res>=0;}inline void print(){    for(int i=1;i<=m;i++){printf("-------i=%d\n",i);        for(int j=0;j<cnt[i];j++){            printf("%d %d \n",A[i][j].pr,A[i][j].k);        }    }}int main(){    int T;    scanf("%d",&T);ios::sync_with_stdio(0);    while(T--){ M.clear();    memset(A,0,sizeof(A));    memset(cnt,0,sizeof(cnt));m = 0;        string str,tmp;int s = 0 , e = 0;        n=0,sum=0,price=0,k=0;        cin>>n>>sum;        while(n--){            cin>>str>>tmp>>price>>k;            e = max(k,e);            if(!M.count(str)){                M[str]=++m;            }int t = M[str];            A[t][cnt[t]++]=obj(price,k);        }for(int i=1;i<=m;i++){            sort(A[i],A[i]+cnt[i],cmp);        }//print();        int mid;        while(s<=e){            mid = (s+e)/2;            if(mid==s){                break;            }//printf("mid=%d\n",mid);            if(judge(mid)){                s = mid;            }else {                e = mid - 1;            }        }if(judge(s+1)){            s++;        }        printf("%d\n",s);    }    return 0;}


0 0