UVALive 3971 Assemble(二分+贪心)

时间:2023-03-10 07:52:50
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 ;
}