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. myeclipse打断点进入后无法查看变量的值的解决方法

    myeclipse打断点进入后无法查看变量的值,打开mycelipse菜单选项:“Window” - “Preferences” - “Java” - “Editor” - “Hovers" ...

  2. USACO 2012 Feb Cow Coupons

    2590: [Usaco2012 Feb]Cow Coupons Time Limit: 10 Sec Memory Limit: 128 MB Submit: 349 Solved: 181 [Su ...

  3. django定义模型类

    模型类被定义在应用文件夹下的model.py中 模型类必须继承Django的models.Model类 属性名不能用连续的两条下划线__ 主键:primary key,简写 pk 不需要主动定义,dj ...

  4. CentOS 7 安装PHP7+Nginx+Mysql5.7开发环境

    安装PHP&PHP-FPM 首先更新一下CentOS7系统,对系统软件做一下升级,这里不升级内核. //使用root权限,注意这里使用upgrade,而不是update(它会升级内核,这里我们 ...

  5. Spring:面向切片编程

    在之前我们记录Spring的随笔当中,都是记录的Spring如何对对象进行注入,如何对对象的属性值进行注入,即我们讲解的很大部分都是Spring的其中一个核心概念——依赖注入(或者说是控制翻转,IOC ...

  6. hdu6086(AC 自动机)

    hdu6086 题意 字符串只由 \(01\) 组成,求长度为 \(2L\) 且包含给定的 \(n\) 个子串的字符串的个数(且要求字符串满足 \(s[i] \neq s[|s| - i + 1]\) ...

  7. SPOJ CIRU - The area of the union of circles (圆的面积并)

    CIRU - The area of the union of circles no tags  You are given N circles and expected to calculate t ...

  8. C++ 对象的内存布局【转】

    单一继承: 虚函数表在最前面的位置. 成员变量根据其继承和声明顺序依次放在后面. 在单一的继承中,被overwrite的虚函数在虚函数表中得到了更新. 多重继承 每个父类都有自己的虚表. 子类的成员函 ...

  9. RPD Volume 172 Issue 1-3 December 2016 评论04 end

    这一篇作为本期的结束是因为发现后面的一些基本上也是EPR有关的会议内容, Contribution of Harold M. Swartz to In VivoEPR and EPR Dosimetr ...

  10. bzoj 2660: [Beijing wc2012]最多的方案

                       Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 617  Solved: 361[Submit][Status][ ...