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. eclipse断点调试时不能进入断点调试

    页面JavaScript代码有错误!!!F12调试.

  2. 6.flume实战(三)※

    需求:将A服务器上的日志实时采集到B服务器上面去 大致原理: 技术选型: exec source + memory channel + avro sink avro source + memory c ...

  3. css左右箭头

    .record-left{ content: ""; width: 0; height: 0; float: left; border-top: 10px solid transp ...

  4. html中的定位与层级设置

    #转载请先留言联系 定位 HTML中的position属性可以对元素进行定位,通过position的不同的值,可以配合方位属性,让元素显示页面中的任何一个位置. position有四个值: stati ...

  5. 使用Bot Service创建Bot Framework

    创建Bot Service:进入至Azure控制台中,新建Bot Service,如不知道Bot Service在哪个选项中,可以先查找Bot Service再创建 在弹出的查询结果中,选择Bot S ...

  6. (2)Python 变量和运算符

    一.python变量特点 python是弱类型语言,无需声明变量可以直接使用并且变量的数据类型可以动态改变 二.变量命名规则 1.不能使用python关键字 2.不能数字开头 3.不能包含空格 4.不 ...

  7. Windows下python的第三方库的安装

    D:\Python27\Scripts\pip.exe install beautifulsoup4

  8. Linux环境安装python3

    linux 安装Python3 1.python下载 请在终端输入如下命令: cd /home wget http://cdn.npm.taobao.org/dist/python/3.6.5/Pyt ...

  9. NOIP2014飞扬的小鸟

    长为n,高为m的二维平面,其中有k个管道(忽略管道的宽度)小鸟始终在游戏界面内移动.从最左边任意高度位置出发,到达游戏界面最右边,游戏完成每个单位时间沿横坐标方向右移距离为1,竖直移动的距离由玩家控制 ...

  10. poj2778(AC 自动机)

    poj2778 题意 构造只包含 \(A, T, C, G\) 的字符串,且满足不出现指定的一些字符串,问长度为 \(n\) 的字符串有多少种 ? 分析 AC 自动机 + 矩阵快速幂的神题 ,知识点很 ...