Hdoj 2333 Assemble 【二分】

来源:互联网 发布:ziperello破解软件 编辑:程序博客网 时间:2024/06/07 21:54

Assemble

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


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
 

题意:你要组装一台电脑,现在给你不同种类的不同品质与价钱的部件,你的任务就是在不超过预算的前提下,从每一种部件中挑选一件,并且输出抽出的部件中最大的品质。

对于这种有最大最小值的题,没有思路时,一般用二分;

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>const int M = 1e3+5;using namespace std;struct node{    char name[25], type[25];    int price, quality;}s[M];int n, b;bool jud(int x){    int i, j, sum = 0, pos, Min;    for(i = 0; i < n;)    {        pos = -1; Min = 0x7fffffff;        for(j = i; j < n&&!strcmp(s[i].name, s[j].name); ++ j)        {            if(Min > s[j].price&&s[j].quality >= x)            {                Min = s[j].price; pos = j;            }        }        if(pos == -1)            return 0;        sum += Min;        i = j;    }    if(sum > b) return 0;    return 1;}int f(int left, int right){    while(left <= right)    {        int mid = (left+right)>>1;          if(jud(mid)) left = mid+1; //这里如果满足就让left = mid+1(因为是要求的最大值)        else right = mid-1;    }    return right;}int main(){    int t;    scanf("%d", &t);    //printf("%d %d\n",Min, Max);    while(t --)    {        scanf("%d%d", &n, &b);        int Min = 0x7fffffff, Max = -0x7fffffff;        for(int i = 0; i < n; ++ i)        {            scanf("%s %s %d %d", s[i].name, s[i].type, &s[i].price, &s[i].quality);  //这里的name与题意的type反了,不过不影响做题。。。            Min = min(Min, s[i].quality);            Max = max(Max, s[i].quality);        }        int ans = f(Min, Max);        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击