zoukankan      html  css  js  c++  java
  • hdoj 2333 Assemble

    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. 

    InputOn 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. 
    OutputPer 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

     1 /*
     2 解题思路:
     3       1. 求解这类"最小值最大"的问题一般方法"二分法". 确定最大和最小的quality, 通过二分
     4          枚举, 判断是否满足最小价钱不超过b价格. 问题自然求解.
     5       2. poj这题数据有点BT, 好多次TLE, 合理使用STL才行. 用map查找配件类型, 875MS时间
     6          不高, 后来网上发现根据quality排序可以在判断是否满足时候提速, 好方法.
     7 */
     8 #include <cstdio>
     9 #include <iostream>
    10 #include <cstring>
    11 #include <string>
    12 #include <vector>
    13 #include <map>
    14 using namespace std;
    15 #define MAX 1005
    16 const int INF = (1<<29);
    17 
    18 struct node{
    19     int price, qu;
    20 }good[MAX][MAX];
    21 
    22 int n, b;
    23 map pos;
    24 int count[MAX];
    25 int num;
    26 
    27 inline int max(int a, int b){
    28     return a > b ? a : b;
    29 }
    30 
    31 inline int min(int a, int b){
    32     return a < b ? a : b;
    33 }
    34 
    35 inline int find(string str){
    36     if( !pos.count(str) ) pos[str] = num++;
    37     return pos[str];
    38 }
    39 
    40 inline bool judge(int qu){
    41     int result = 0;
    42     for(int i = 0; i < num; ++i){
    43         int minOne = b+1;
    44         for(int j = 0; j < count[i]; ++j){
    45             if( good[i][j].qu >= qu )
    46                 minOne = min(minOne, good[i][j].price);
    47         }
    48         result += minOne;
    49         if(result > b) return false;
    50     }
    51     return true;
    52 }
    53 
    54 int main(){
    55     int caseNum;
    56     scanf("%d", &caseNum);
    57     while(caseNum--){
    58         scanf("%d %d", &n, &b);
    59         num = 0;
    60 
    61         memset(count, 0, sizeof(count));
    62         pos.clear();
    63         int left = INF, right = 0;
    64         int price, qu;
    65         char name[22], type[22];
    66         for(int i = 0; i < n; ++i){
    67             scanf("%s %s %d %d", type, name, &price, &qu);
    68             right = max(right, qu);
    69             left = min(left, qu);
    70             int temp = find(type);
    71             good[temp][count[temp]].price = price;
    72             good[temp][count[temp]++].qu = qu;
    73         }
    74 
    75         while(left <= right){
    76             int mid = (left+right)>>1;
    77             if( judge(mid) ) left = mid+1;
    78             else right = mid-1;
    79         }
    80         printf("%d
    ", right);
    81     }
    82     return 0;
    83 }


  • 相关阅读:
    PHP新建文件并写入内容demo
    PHP输出结果demo
    网站顶部图标使用分析
    .htaccess文件的创建 / 联动天下空间伪静态(isapi_rewrite)配置方法
    【原创】网站底部竖线布局对比/研究
    JS判断访问设备终端PC/iPad/iPhone/android 和浏览器IE/Opera/Firefox/webKit
    我的blog风格
    2020.11.17 近日复健情况
    南风又起,锦字题予往昔(写给校刊《我们大多数》)
    【对拍大法好!!】
  • 原文地址:https://www.cnblogs.com/z-712/p/7324493.html
Copyright © 2011-2022 走看看