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. ORACLE导入、导出所有数据到文件的SQL语句

    打开cmd窗口,执行如下SQL语句即可 --导出 exp 用户名/密码@localhost/orcl file=d:\111.dump log=d:111.log --或者 1.登录管理员system ...

  2. 【C++】指针和new相关

    看黄邦勇帅的笔记. 指针和new之前觉得已经掌握的很好了,可是看了资料还是get到了新知识.记录一下. 1.指针只支持 4 种算术运算符:++,――,+,-.指针只能与整数加减.指针运算的原则是:每当 ...

  3. Redis Hlen 命令用于获取哈希表中字段的数量

    http://www.runoob.com/redis/hashes-hlen.html

  4. 如何php防止XSS攻击

    什么是XSS:这里通俗的讲,就像是SQL注入一样,XSS攻击也可以算是对HTML和JS的一种注入.你本来希望得到是从用户那得到一段有用的文本文字,但用户提交给你的却是别有用心的可执行javascrip ...

  5. java 编码分析

    三.源码分析:    更改字符串编码的步骤为:    1.调用String的getByte方法对字符串进行解码,得到字符串的字节数组(字节数组不携带任何有关编码格式的信息,只有字符才有编码格式)    ...

  6. (2)三剑客之grep

    1)grep和egrep定义grep:在文件中全局查找指定的正则表达式,并打印所有包含该表达式的行egrep:扩展的egrep,支持更多的正则表达式元字符2)命令格式语法:grep [选项] patt ...

  7. 计蒜客 30999.Sum-筛无平方因数的数 (ACM-ICPC 2018 南京赛区网络预赛 J)

    J. Sum 26.87% 1000ms 512000K   A square-free integer is an integer which is indivisible by any squar ...

  8. Codeforces 1023 C.Bracket Subsequence-STL(vector) (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)

    C. Bracket Subsequence ... 代码: 1 //C 2 #include<iostream> 3 #include<cstdio> 4 #include& ...

  9. luogu P3919 【模板】可持久化数组(可持久化线段树/平衡树)

    As you see // luogu-judger-enable-o2 #include<cstdio> #include<cstring> #include<algo ...

  10. 【CCpp程序设计2017】迷宫游戏

    大一寒假作业!写了第一个小游戏! //maze_test By lizitong #include<stdio.h> #include<time.h> #include< ...