UVALive 3971

题意:有b块钱。想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子。若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子。

思路:

求最大的最小值一般用二分法。

在(0。maxq)内进行二分,判定q作为最差品质因子是否可行。

大白书原题。比較考验代码功底。

code:

/*
* @author Novicer
* language : C++/C
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint; const int maxn = 1000 + 5; map<string,int> id;
int n,b;
int cnt = 0; struct Comp{
int price;
int quality;
}; vector<Comp> comp[maxn]; int ID(string s){
if(!id.count(s)){
id[s] = cnt++;
}
return id[s];
} bool ok(int q){
int sum = 0;
// cout << q << endl;
for(int i = 0 ; i < cnt ; i++){
int cheapest = b + 1;
for(int j = 0 ; j < comp[i].size() ; j++){
// cout << comp[i][j].price << endl;
if(q <= comp[i][j].quality) cheapest = min(cheapest , comp[i][j].price);
}
if(cheapest == b+1) return false;
sum += cheapest;
// cout << "q : " << q << " sum : " << sum << endl;
if(sum > b) return false;
}
return true;
}
int solve(int l , int r){
while(l < r){
int m = l + (r - l + 1)/2;
if(ok(m)) l = m;
else r = m - 1;
// cout << m << endl;
}
return l;
}
int main(){
// freopen("input.txt","r",stdin);
int T;
cin >> T;
while(T--){
cnt = 0;
cin >> n >> b;
for(int i = 0 ; i < n ; i++) comp[i].clear();
int maxq = 0;
for(int i = 1 ; i <= n ; i++){
string type,name;
int p,q;
cin >> type >> name >> p >> q;
// cout << type << name << p << q << endl;
maxq = max(maxq , q);
Comp tmp;
tmp.price = p; tmp.quality = q;
comp[ID(type)].push_back(tmp);
}
// cout << maxq << endl;
int L = 0 , R = maxq;
int ans = solve(L,R);
cout << ans << endl;
}
return 0;
}

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1972">

UVALive 3971 Assemble(模拟 + 二分)的更多相关文章

  1. UVALive 3971 Assemble(二分+贪心)

    本题思路不难,但是要快速准确的AC有点儿考验代码功力. 看了大白书上的标程,大有所获. 用map和vector的结合给输入分组,这个数据结构的使用非常精美,恰到好处. #include<iost ...

  2. uvalive 3971 - Assemble(二分搜索 + 贪心)

    题目连接:3971 - Assemble 题目大意:有若干个零件, 每个零件给出的信息有种类, 名称, 价格, 质量,  现在给出一个金额, 要求在这个金额范围内, 将每个种类零件都买一个, 并且尽量 ...

  3. UVaLive 3971 Assemble (水题二分+贪心)

    题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...

  4. UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)

    先从中找出性能最好的那个数, 在用钱比較少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #incl ...

  5. uvalive 3971 Assemble

    https://vjudge.net/problem/UVALive-3971 题意: 现在你要组装一台电脑,每个电脑的一种类型的配件都有多种选择,它们的名字是不同的. 现在给出已有的元件,每种类型都 ...

  6. LA 3971 Assemble(二分)

    题目: 给你b元钱,让你组装一台电脑,有n个配件,属性有 种类 名字 价格 品质,每种类型选至少一个,并且最小品质最大.输出这个最大的最小品质. 白书上说了,最小值最大的问题一般是二分来求解答案.在这 ...

  7. UVALive - 3211 (2-SAT + 二分)

    layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...

  8. FZU 1575 小学生的游戏【模拟二分】

    某天,无聊的小斌叫上几个同学玩游戏,其中有比较笨的小兴,比较傻的小雪,可爱的小霞和自以为是的小楠.他们去找聪明的小明去给他们当裁判.判定谁取得游戏胜利. 而这个游戏是由小斌想个1到10000000的数 ...

  9. Uva 12124 Uva Live 3971 - Assemble 二分, 判断器, g++不用map.size() 难度:0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

随机推荐

  1. 关于测试url传值的问题

    url1:http://localhost:8080/fms/finan/isRiskCustomer.action?customername="xxxxxxxx"; 如上这样写, ...

  2. python 多进程multiprocessing 模块

    multiprocessing 常用方法: cpu_count():统计cpu核数 multiprocessing.cpu_count() active_children() 获取所有子进程 mult ...

  3. DELPHI用const来提高应用程序在多核多线程下的性能

    来自:http://bbs.csdn.net/topics/330048800 --------------------------------------------------------- 我们 ...

  4. solr params.json

    The Request Parameters API allows creating parameter sets that can override or take the place of par ...

  5. 子类构造函数 supper关键字

    在导出类的构造函数,如果没有明确指定调用哪一个基类构造器,它会默默调用默认构造器. 如果不存在默认构造器,编译器就会报错. java编程思想 p158(p194)

  6. DevExpress控件-GridControl根据条件改变单元格/行颜色(Dev GridControl 单元格着色) z

    DevExpress控件-数据控件GridControl,有时我们需要根据特定条件改变符合条件的行或者单元格颜色达到突出显示目的,现在动起鼠标跟我一起操作吧,对的,要达到这个目的您甚至都不用动键盘. ...

  7. 上传文件提示413 Request Entity Too Large错误

    打开nginx主配置文件nginx.conf 一般在/usr/local/nginx/conf/nginx.conf这个位置 找到http{}段并修改以下内容 client_max_body_size ...

  8. PHP无限极分类详谈

    当你学习php无限极分类的时候,大家都觉得一个字“难”我也觉得很难,所以,现在都还在看,因为工作要用到,所以,就必须得研究研究. 到网上一搜php无限极分类,很多,但好多都是一个,并且,写的很乱,代码 ...

  9. 理解和上手Redux

    顾名思义本文分两个部分,理解和上手,第一部分我先讲个故事,这个故事也许不是特别形象,但对大家理解Redux一定有所帮助.第二部分我举个例子. 先讲个故事: 一个餐厅(应用),我是顾客(用户),这个餐厅 ...

  10. fmod()函数和modf()函数

    最近从博客上看到了一个fmod函数,结果又蹦出来一个modf函数 fmod()函数: 头文件:#include<math.h> C库函数... fmod()用来对浮点数进行取模(求余),原 ...