首页 > 其他 > 详细

Hdoj 2333 Assemble 【二分】

时间:2015-04-25 22:47:05      阅读:308      评论:0      收藏:0      [点我收藏+]

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

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

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

代码:

#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;
}


Hdoj 2333 Assemble 【二分】

原文:http://blog.csdn.net/shengweisong/article/details/45276329

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!