UVA之12124 - Assemble

简介:

【题目】

Problem A - Assemble

Time limit: 2 seconds

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

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
The 2007 ACM Northwestern European Programming Contest

【分析】

这是一个常见的最小值最大的问题,解决该问题常用方法为二分答案。

假设答案为x,如何判断这个x是最小还是最大呢?删除品质因子小于x的所有配件,如果可以组装出一台不超过b元的电脑,那么

标准答案ans 大于等于x,否则小于x.

【代码】

/*********************************
*   日期:2014-4-17
*   作者:SJF0115
*   题号: Assemble
*   来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_problem&problem=3276
*   来源:UVA
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <string>
using namespace std;


int b;//预算
const int maxn = 1000 + 1;

//配件实体
struct Component{
    int price;
    int quality;
};
//vector类型的数组Comp
vector<Component> comp[maxn];
//配件种类,同一配件种类同一Id
map<string,int> id;
//配件的类型数
int cnt;
//获取配件种类的Id
int GetCompId(string type){
    //不包含
    if(!id.count(type)){
        id[type] = cnt;
        cnt ++;
    }
    return id[type];
}
//判断不小于mid的品质因子是否超过预算
bool isOK(int mid){
    int i,j,sum=0;
    //共有cnt个不同种类的配件
    for(i = 0;i < cnt;i++){
        int cheapest = b + 1;
        int size = comp[i].size();
        //在品质因子小于等于mid的前提下找一个最低价
        for(j = 0;j < size;j++){
            if(comp[i][j].quality >= mid){
                if(comp[i][j].price < cheapest){
                    cheapest = comp[i][j].price;
                }
            }
        }//for
        //当前种类的配件没有一个满足条件的
        if(cheapest == b+1){
            return false;
        }
        sum += cheapest;
        //当前花费超过预算
        if(sum > b){
            return false;
        }
    }//for
    return true;
}

int main(){
    int T,i,j,n;
    scanf("%d",&T);
    //T组测试数据
    while(T--){
        //配件数目与预算
        scanf("%d %d",&n,&b);
        //配件数目
        cnt = 0;
        //初始化
        for(i = 0;i < n;i++){
            comp[i].clear();
        }
        id.clear();
        //最大品质因子
        int maxq = 0;
        //n个配件描述
        for(i = 0;i < n;i++){
            char type[30],name[30];
            int price,quality;
            scanf("%s %s %d %d",type,name,&price,&quality);
            //更新最大品质因子
            if(quality > maxq){
                maxq = quality;
            }
            int id = GetCompId(type);
            //comp[i]存储不同种类的配件
            comp[id].push_back(Component{price,quality});
        }
        //二分搜索
        int L = 0,R = maxq;
        while(L < R){
            int mid = L+(R - L + 1) / 2;
            if(isOK(mid)){
                L = mid;
            }
            else{
                R = mid - 1;
            }
        }
        printf("%d\n",L);
    }
    return 0;
}



目录
相关文章
|
9月前
Uva10001 Garden of Eden
Uva10001 Garden of Eden
33 0
|
9月前
uva10038 Jolly Jumpers
uva10038 Jolly Jumpers
23 0
|
7月前
uva 10340 all in all
输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变),得到字符串是。
21 0
|
9月前
UVa11968 - In The Airport
UVa11968 - In The Airport
37 0
|
9月前
UVa11776 - Oh Your Royal Greediness!
UVa11776 - Oh Your Royal Greediness!
39 0
|
9月前
uva10112 Myacm Triangles
uva10112 Myacm Triangles
25 0
UVa 10082 WERTYU
Problem Description A common typing error is to place the hands on the keyboard one row to the right of the correct position.
863 0
|
机器学习/深度学习
|
算法
UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494...
1540 0