UVALive 3971 Assemble(二分+贪心)
本题思路不难,但是要快速准确的AC有点儿考验代码功力。
看了大白书上的标程,大有所获。
用map和vector的结合给输入分组,这个数据结构的使用非常精美,恰到好处。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const double eps=1e-;
const int INF=;
const int maxn=+;
int T,n,b,cnt;
struct component
{
int price,quality;
component(int x,int y):price(x),quality(y) {}
};
map<string,int>id;
int getid(string str)
{
if(!id.count(str)) id[str]=cnt++;
return id[str];
}
vector<component>v[maxn];
void ini()
{
cnt=;
for(int i=; i<n; i++) v[i].clear();
id.clear();
}
bool ok(int x)
{
LL sum=;
for(int i=; i<cnt; i++)
{
int cheap=b+;
int siz=v[i].size();
for(int j=; j<siz; j++)
{
if(v[i][j].quality>=x)
{
cheap=min(cheap,v[i][j].price);
}
}
if(cheap==b+) {return false;}
else sum+=(LL)cheap;
}
if(sum<=(LL)b) {return true;}
else {return false;}
}
int main()
{
//freopen("in1.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&b);
ini();
int maxqq=-;
for(int i=; i<n; i++)
{
string type,name;
int pp,qq;
cin>>type>>name>>pp>>qq;
maxqq=max(qq,maxqq);
v[getid(type)].push_back(component(pp,qq));
}
int L=,R=maxqq;
while(L<R)
{
//cout<<L<<"hhh"<<R<<endl;
int M=L+(R-L+)/;
/*这么写是向上进位的写法,而M=(L+R)/2是
向下取整的写法,写成哪个要根据实际
情况(也就是二分的写法)来,
本题如果写成第二个就会出现死循环,比如L=1,R=2
时就是。*/
if(ok(M))
{
L=M;
}
else
{
R=M-;
}
}
printf("%d\n",L);
}
//fclose(stdin);
//fclose(stdout);
return ;
}
UVALive 3971 Assemble(二分+贪心)的更多相关文章
- UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)
先从中找出性能最好的那个数, 在用钱比較少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #incl ...
- uvalive 3971 - Assemble(二分搜索 + 贪心)
题目连接:3971 - Assemble 题目大意:有若干个零件, 每个零件给出的信息有种类, 名称, 价格, 质量, 现在给出一个金额, 要求在这个金额范围内, 将每个种类零件都买一个, 并且尽量 ...
- UVALive 3971 Assemble(模拟 + 二分)
UVALive 3971 题意:有b块钱.想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子.若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子. 思路: 求最大的最小值一 ...
- UVaLive 3971 Assemble (水题二分+贪心)
题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...
- uvalive 3971 Assemble
https://vjudge.net/problem/UVALive-3971 题意: 现在你要组装一台电脑,每个电脑的一种类型的配件都有多种选择,它们的名字是不同的. 现在给出已有的元件,每种类型都 ...
- 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 ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心
题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...
随机推荐
- R语言中abline和lines的区别
函数lines()其作用是在已有图上加线,命令为lines(x,y),其功能相当于plot(x,y,type="1")函数abline()可以在图上加直线,其使用方法有四种格式.( ...
- 剑指offer 面试67题
面试67题: 题目: 链接:https://www.nowcoder.com/questionTerminal/1277c681251b4372bdef344468e4f26e?commentTags ...
- Python 函数名作为字典值
Python中是没有switch的, 所以有时我们需要用switch的用法, 就只能通过if else来实现了. 但if else写起来比较冗长, 这时就可以使用Python中的dict来实现, 比s ...
- jquery 字符串转为json
使用ajax从服务器拿到的数据,jquery总是认为是字符串,无法直接使用,可以通过下面代码转换: $.get("服务器路径", function(data) { data = e ...
- 异动K线--庄家破绽
<异动K线--庄家破绽(连载)> http://bbs.tianya.cn/post-stocks-612892-1.shtml ————马后炮分析,没有什么前瞻性.纯技术是害死许多钻牛角 ...
- 搭建backup服务器基本流程
守护进程实现,将daemon配置在backup服务器,因为这样其他服务器就能通过服务推即可. 服务端配置流程: 前提两台服务41为backup服务 31是其他服务器即客户端 在41服务器中配置 ...
- 【c++习题】【17/5/22】重载数组下标操作符
一.写出程序运行结果 1#include <iostream > using namespace std; int a[10]={1,2, 3, 4, 5, 6, 7, 8, 9, 10} ...
- Django组件 用户认证,form
auth模块 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中: 如果用户存在于数据库中,然后再验证用户输入的密码,这样一来就要自己编写大量的代码 ...
- HISAT2的运用
功能: 用于有参考基因组存在的比对工具(适用于whole-genome, transcriptome, and exome sequencing data) 用法: hisat2-build [opt ...
- springmvc拦截器基本使用
1.HandlerExecutionChain是一个执行链,当用户的请求到达DispatcherServlet的时候,DispatcherServlet会到HandlerMapping中查找对应的Ha ...