本题思路不难,但是要快速准确的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(二分+贪心)的更多相关文章

  1. UVA 12124 UVAlive 3971 Assemble(二分 + 贪心)

    先从中找出性能最好的那个数, 在用钱比較少的去组合,能组出来就表明答案在mid的右边,反之在左边, #include<string.h> #include<map> #incl ...

  2. uvalive 3971 - Assemble(二分搜索 + 贪心)

    题目连接:3971 - Assemble 题目大意:有若干个零件, 每个零件给出的信息有种类, 名称, 价格, 质量,  现在给出一个金额, 要求在这个金额范围内, 将每个种类零件都买一个, 并且尽量 ...

  3. UVALive 3971 Assemble(模拟 + 二分)

    UVALive 3971 题意:有b块钱.想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子.若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子. 思路: 求最大的最小值一 ...

  4. UVaLive 3971 Assemble (水题二分+贪心)

    题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...

  5. uvalive 3971 Assemble

    https://vjudge.net/problem/UVALive-3971 题意: 现在你要组装一台电脑,每个电脑的一种类型的配件都有多种选择,它们的名字是不同的. 现在给出已有的元件,每种类型都 ...

  6. 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 ...

  7. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  8. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  9. 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心

    题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...

随机推荐

  1. Network Basic Knowledge

    @1: 应用层的常用协议以及对应的端口号: DNS 53/tcp/udp SMTP 25/tcp POP3 110/tcp HTTP 80/tcp HTTPS 443/udp TELNET 23/tc ...

  2. range基础

    collapse这个方法是把结束位置抛弃掉,并不是简单的设置到开始位置. 结束位置被抛弃掉以后,只要没有给它重新设置位置,它就一直都会等 于开始位置.即使你修改了开始位置,结束位置还是会在修改后的开始 ...

  3. flex label 换行

    Flex中label换行有两种情况 在AS中赋值: label.text="Online\r\nResources" 在mxml中赋值: text="Online Res ...

  4. 【leetcode刷题笔记】Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  5. php的正则表达式

    这篇文章介绍的内容是关于php的正则表达式 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下. 正则表达式是一种描述字符串结果的语法规则,是一个特定的格式化模式,可以匹配.替换.截取匹配 ...

  6. CCNA 课程 四

    Vlan基础: Vlan的作用:把物理上分割的用户,让他们逻辑上在一起. Vlan 范围: 0- 4095 0  4095 是保留的 不可以使用 1 cisco 本证vlan 标准vlan 1 -10 ...

  7. mysql里的ibdata1文件

    mysql大多数磁盘空间被 InnoDB 的共享表空间 ibdata1 使用.而你已经启用了 innodb_file_per_table,所以问题是: ibdata1存了什么? 当你启用了innodb ...

  8. MVC 绑定 下拉框数据

    HTML: <div class="form-group col-sm-12"> <div class="col-sm-4"> < ...

  9. VC 写注册表

    BOOL Running() { HKEY hKey; LPCTSTR strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion ...

  10. Educational Codeforces Round 15 A, B , C 暴力 , map , 二分

    A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...