閱讀526 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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
*   來源:https://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;
}



最後更新:2017-04-03 12:56:18

  上一篇:go Android-Universal-Image-Loader圖片異步加載並緩存
  下一篇:go android超炫的動畫效果