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



目录
相关文章
|
JSON Dart 安全
Flutter App混淆加固、保护与优化原理
Flutter App混淆加固、保护与优化原理
238 0
|
机器学习/深度学习 存储 数据可视化
NumPy 1.26 中文官方指南(二)(4)
NumPy 1.26 中文官方指南(二)
289 1
|
10月前
|
JavaScript 前端开发 API
Vue 3 中 v-model 与 Vue 2 中 v-model 的区别是什么?
总的来说,Vue 3 中的 `v-model` 在灵活性、与组合式 API 的结合、对自定义组件的支持等方面都有了明显的提升和改进,使其更适应现代前端开发的需求和趋势。但需要注意的是,在迁移过程中可能需要对一些代码进行调整和适配。
442 60
|
9月前
|
监控 测试技术 定位技术
HTTP代理IP响应速度测试方案设计与指标体系
随着数字化发展,网络安全、隐私保护及内容访问自由成为核心需求。HTTP代理因其技术优势成为热门选择。本文介绍HTTP代理IP响应速度测试方案,包括基础性能、稳定性、地理位置、实际应用、安全性测试及监控指标,推荐测试工具,并提供测试结果评估标准。
182 2
|
Java
【Java系列】if-else代码优化的八种方案
目录 前言 优化方案一:提前return,去除不必要的else 优化方案二:使用条件三目运算符 优化方案三:使用枚举 优化方案四:合并条件表达式 优化方案五:使用 Optional 优化方案六:表驱动法 优化方案七:优化逻辑结构,让正常流程走主干 优化方案八:策略模式+工厂方法消除if else 前言 代码中如果if-else比较多,阅读起来比较困难,维护起来也比较困难,很容易出bug,接下来,本文将介绍优化if-else代码的八种方案。 优化方案一:
900 0
【Java系列】if-else代码优化的八种方案
|
11月前
|
监控 数据可视化 测试技术
软件测试中的自动化测试实践指南
【10月更文挑战第7天】 在软件开发的生命周期中,测试是确保产品质量的重要环节。随着技术的进步和应用的复杂性增加,自动化测试逐渐成为提升测试效率和覆盖范围的关键手段。本文将深入探讨自动化测试的基本概念、实施步骤及其在不同应用场景中的最佳实践。通过对自动化测试框架的选择、脚本开发、执行及维护的详细解析,帮助读者更好地理解和应用自动化测试技术,从而优化测试流程,提高软件质量。
135 2
|
11月前
|
监控 应用服务中间件 nginx
详细解释容器以及虚拟机centos7.9容器化部署基础服务(容器化部署nginx)
容器是一种轻量级、可移植的软件打包和隔离技术,将应用程序及其依赖项打包,确保在任何环境中一致运行。容器共享主机操作系统内核,相比虚拟机更高效、轻量,具有快速启动和高资源利用率的特点。容器的关键技术包括命名空间(如 PID、NET 等)、控制组(cgroups)和联合文件系统(UnionFS)。使用容器可以提高开发和部署效率,简化管理,确保环境一致性。例如,在 CentOS 7.9 上部署 Nginx 时,可以通过 Docker 下载和运行 `nginx:1.20` 镜像,并通过端口映射使外部请求访问 Nginx 服务。此外,还可以将测试页面复制到容器中,进一步验证容器的功能。
296 0
|
前端开发 JavaScript
Threejs入门进阶实战案例(3):视频贴图的解决方案
Threejs入门进阶实战案例(3):视频贴图的解决方案
467 0
|
Ubuntu Linux 数据安全/隐私保护
Linux系统使用Docker部署Cloudreve云盘并实现远程访问
Linux系统使用Docker部署Cloudreve云盘并实现远程访问
269 0
|
运维 监控 Java
微服务心跳监测机制讲解与实现,与面试过程中如何回答这个问题
微服务心跳监测机制讲解与实现,与面试过程中如何回答这个问题
296 0