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. 1
  2. 18 800
  3. processor 3500_MHz 66 5
  4. processor 4200_MHz 103 7
  5. processor 5000_MHz 156 9
  6. processor 6000_MHz 219 12
  7. memory 1_GB 35 3
  8. memory 2_GB 88 6
  9. memory 4_GB 170 12
  10. mainbord all_onboard 52 10
  11. harddisk 250_GB 54 10
  12. harddisk 500_FB 99 12
  13. casing midi 36 10
  14. monitor 17_inch 157 5
  15. monitor 19_inch 175 7
  16. monitor 20_inch 210 9
  17. monitor 22_inch 293 12
  18. mouse cordless_optical 18 12
  19. mouse microsoft 30 9
  20. keyboard office 4 10

Sample Output

  1. 9
The 2007 ACM Northwestern European Programming Contest
 
题意:你要去自己买个组装机,现在给你每个零件的类别、名字、价钱、级别,以及你有的钱数,求能组装成的机器的最大级别(机器的所有零件中的最小级别,即最小值最大)。
 
解决“最小值最大”问题的常用方法是“二分答案”。
何为二分答案?以本题为例,假设机器的所有零件中的最小级别为x,删除级别小于x的所有配件,若可以组装成一台不超过b元的电脑,那么ans≥x, 否则ans<x;
 
至于如何判断是否可以组装出不超过b元的电脑。利用贪心的思路,每种配件选择最便宜的一个即可。
 
二分部分代码:
  1. while(L < R)
  2. {
  3. int M = L+(R-L+)/;
  4. if(ok(M)) L = M;
  5. else R = M-;
  6. }

贪心部分代码:

  1. bool ok(int q)
  2. {
  3. int sum = ;
  4. for(int i = ; i < cnt; i++)
  5. {
  6. int sz = comp[i].size();
  7. int cheapest = b+;
  8. for(int j = ; j < sz; j++)
  9. {
  10. if(comp[i][j].quality >= q)
  11. cheapest = min(cheapest, comp[i][j].price);//选择最便宜的
  12. }
  13. if(cheapest == b+) return false;
  14. sum += cheapest;
  15. if(sum > b) return false;
  16. }
  17. return true;
  18. }

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<vector>
  7. #include<map>
  8. using namespace std;
  9. const int maxn = ;
  10.  
  11. struct Component
  12. {
  13. int price, quality;
  14. };
  15. vector<Component> comp[maxn];
  16. int n, b, cnt;
  17.  
  18. map<string, int> id;
  19. int type_ID(string s)
  20. {
  21. if(!id.count(s)) id[s] = cnt++;
  22. return id[s];
  23. }
  24. bool ok(int q)
  25. {
  26. int sum = ;
  27. for(int i = ; i < cnt; i++)
  28. {
  29. int sz = comp[i].size();
  30. int cheapest = b+;
  31. for(int j = ; j < sz; j++)
  32. {
  33. if(comp[i][j].quality >= q)
  34. cheapest = min(cheapest, comp[i][j].price);
  35. }
  36. if(cheapest == b+) return false;
  37. sum += cheapest;
  38. if(sum > b) return false;
  39. }
  40. return true;
  41. }
  42. int main()
  43. {
  44. int T; scanf("%d", &T);
  45. while(T--)
  46. {
  47. scanf("%d%d", &n, &b);
  48. int maxq = ; cnt = ; id.clear();
  49. for(int i = ; i < n; i++) comp[i].clear();
  50. for(int i = ; i < n; i++)
  51. {
  52. char type[], name[]; scanf("%s%s", type, name);
  53. int pri, qua; scanf("%d%d", &pri, &qua);
  54. maxq = max(qua, maxq);
  55. Component tmp; tmp.price = pri, tmp.quality = qua;
  56. string tp(type);
  57. comp[type_ID(tp)].push_back(tmp);
  58. }
  59. int L = , R = maxq;
  60. while(L < R)
  61. {
  62. //cout << "-----" <<endl;
  63. int M = L+(R-L+)/;
  64. if(ok(M)) L = M;
  65. else R = M-;
  66. }
  67. printf("%d\n", L);
  68. }
  69. return ;
  70. }

【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)的更多相关文章

  1. 洛谷3933 Chtholly Nota Seniorious 二分答案+贪心

    题目链接 题意 给你一个N*M的矩阵 (N,M <=2000)  把他分成两部分 使两部分的极差较大的一个最小  求这个最小值.然后分矩阵的要求是:每个部分内部的方块之间,可以通过上下左右相互到 ...

  2. BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心

    BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心 Description Bessie烘焙了一块巧克力蛋糕.这块蛋糕是由R*C(1 <= R,C ...

  3. 【二分答案+贪心】UVa 1335 - Beijing Guards

    Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City ...

  4. [CSP-S模拟测试]:kill(二分答案+贪心)

    题目传送门(内部题50) 输入格式 第一行包含四个整数$n,m,s$,表示人数.怪物数及任务交付点的位置.第二行包含$n$个整数$p_1,p_2,...,p_n$.第三行包含$n$个整数$q_1,q_ ...

  5. UVA 12124 Assemble(二分答案)

    题目链接:https://vjudge.net/problem/UVA-12124 垃圾vjudge毁我青春!! 首先这道题是解决“最小值最大”的问题,所以要二分答案. 在这里我们二分$quality ...

  6. 【洛谷】【二分答案+贪心】P1316 丢瓶盖

    [题目描述:] 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? [ ...

  7. BZOJ5321 JXOI2017加法(二分答案+贪心+堆+树状数组)

    二分答案后得到每个位置需要被加的次数.考虑贪心.从左到右考虑每个位置,将以该位置为左端点的区间按右端点从大到小加进堆.看该位置还需要被加多少次,如果不需要加了就不管,否则取堆顶区间将其选择,BIT实现 ...

  8. Gym 100886J Sockets 二分答案 + 贪心

    Description standard input/outputStatements Valera has only one electrical socket in his flat. He al ...

  9. bzoj 4310 跳蚤 —— 后缀数组+二分答案+贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4310 二分答案——在本质不同的子串中二分答案! 如果二分到的子串位置是 st,考虑何时必须分 ...

随机推荐

  1. linux内核-双向链表

    linux中的经典宏定义 offsetof 定义:offsetof在linux内核的include/linux/stddef.h中定义. #define offsetof(TYPE, MEMBER) ...

  2. usb 驱动

    usb 驱动学习总结: usb 采用分层的拓扑结构,金字塔型,最多是7层.usb 是主从结构,主和主或者从和从之间不能交换数据.理论上一个usb主控制器最多可接127个设备,协议规定每个usb设备具有 ...

  3. Java序列化之transient和serialVersionUID的使用

    package FileDemo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IO ...

  4. python 使用__future__

    Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动.有些改动是不兼容旧版本的,也就是在当前版本运行正常的代码,到下一个版本运行就可能不正常了. 从Python 2.7到Pytho ...

  5. hdu 5585 Numbers【大数+同余定理】

    Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  6. mssql函数demo

    ALTER FUNCTION [dbo].[f_GetCookType] (@saleDate datetime)RETURNS varchar(6)ASBEGIN declare @cookType ...

  7. nginx+tomcat+redis负载均衡及session共享

    概述 本文档是用来详细描述 nginx+tomcat+redis负载均衡实现session共享 所需软件及下载地址 软件名称 下载地址 功能说明 Nginx-v1.6.0 http://nginx.o ...

  8. SAP PP 生产订单变更记录保存

    *&---------------------------------------------------------------------* *& 包括 ZXCO1U01 *&am ...

  9. 注入限制绕过<转>

    突然想我们是否可以用什么方法绕过SQL注入的限制呢?到网上考察了一下,提到的方法大多都是针对AND与“'”号和“=”号过滤的突破,虽然有点进步的地方,但还是有一些关键字没有绕过,由于我不常入侵网站所以 ...

  10. Thread学习

    1.定义 2.作用 3.和进程的比较 4.多线程(multithreading)的优点和缺陷 5.调度(scheduling) 6.线程相关概念 定义 线程就是最小的可编程指令序列,是进程的子集.一个 ...