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. 3.shell位置参数变量

    当我们执行一个shell脚本时,希望可以获取命令行里的参数信息,就可以使用位置参数变量.比如 sh ./a.sh 100 200,就是一个执行shell的命令行,可以在a.sh脚本中获取到参数信息 语 ...

  2. 【 Zabbix 】— 监控nginx

    一.环境说明 OS:centos6.7 x64 nginx:nginx/1.9.9 ZABBIX:2.4.8 zabbix监控nginx是根据nginx的stub_status模块,抓取status模 ...

  3. CSS中的HSLA颜色

    CSS 中的颜色可以由RGB色彩空间和HSL色彩空间两种方式来表述.其中我们常用的是RGB色彩空间,RGB色彩空间的颜色表示方式有:十六进制颜色(如红色:#FF0000).RGB颜色(如红色:rgb( ...

  4. tcpdump学习(2):基本使用

    安装好tcpdump之后,运行tcpdump: 1. tcpdump -D 获取网络适配器列表,以下是在Ubuntu上获取到的结果: root@holmesian-laptop:~# tcpdump ...

  5. [libgdx游戏开发教程]使用Libgdx进行游戏开发(6)-添加主角和道具

    如前所述,我们的主角是兔子头.接下来我们实现它. 首先对AbstractGameObject添加变量并初始化: public Vector2 velocity; public Vector2 term ...

  6. PHP实现支付宝即时到账功能

    本文实例为大家分享了PHP支付宝即时到账功能的实现代码,供大家参考,具体内容如下 首先需要下载即时到账交易接口,传送门https://doc.open.alipay.com/doc2/detail?t ...

  7. ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛

    编号 名称 通过率 通过人数 提交人数 A√水题(队友写的 Visiting Peking University 91% 1122 1228 B— Reverse Suffix Array 57% 6 ...

  8. 树状数组优化DP 【模拟赛】删区间

    哇,难受得一匹. 看到题的一瞬间竟然只想到了\(n^3\)的区间\(DP\) 一.\(40pts\) 设\(f[i][j]\)代表删去\(i\)到\(j\)这一段区间的最小代价和. 然后直接写普通的区 ...

  9. Ubuntu开机启动的方式

    方法一:--------------------------------------------------1. 在/etc/init.d/下放置启动脚本,比如postgresqlroot@ubunt ...

  10. [BZOJ1513]Tet-Tetris 3D

    get了新的标记永久化技能- 这题要求询问max和覆盖,因为是线段树套线段树,所以内外都不可以标记下传 这种标记永久化的套路是维护两个标记:$mx,all$,$mx$表示这个子树内的真最大值,$all ...