Assemble UVA - 12124
题意:
给你b元去选购配件。有n个配件,每个配件都有种类、名字、品质因子、价格。
要求
每种配件购买一个,总价格不超过b,且最差配件的品质因子的应尽可能大。
思路:
典型二分答案。
反思:
二分时假如是
w
h
i
l
e
(
l
<
=
r
)
while(l<=r)
while(l<=r)
那么中间要
l
=
m
i
d
+
1
l=mid+1
l=mid+1 和
r
=
m
i
d
−
1
r=mid-1
r=mid−1
二分时假如是
w
h
i
l
e
(
l
<
r
)
while(l<r)
while(l<r)
那么中间要
l
=
m
i
d
l=mid
l=mid 和
r
=
m
i
d
−
1
r=mid-1
r=mid−1
AC
#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
int n, b;
const int maxn = 1e3+10;
struct compent{
int pr, qa;
};
vector<compent> pp[maxn];
vector<string> tpp;
map<string,int>ma;
int idc(char * type){
string s(type);
if(ma.count(s))return ma[s];
tpp.pb(s);
return ma[s] = sz(tpp)-1;
}
void init(){
For(i,0,n)pp[i].clear();
ma.clear();
tpp.clear();
}
bool check(int x){
int res = 0, tot = sz(tpp);
fori(i,0,tot){
int tot1 = sz(pp[i]), mi = 1e9+10;
fori(j,0,tot1){
if(pp[i][j].qa>=x)mi = min(mi,pp[i][j].pr);
}
res += mi;
if(res>b)return false;
}
return true;
}
int main()
{
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tt; scanf("%d", &tt);
while(tt--){
int pr, qa, idx;
char name[30], type[30];
scanf("%d%d", &n, &b);
init();
int l, r=0, ans;
For(i,1,n){
scanf("%s%s%d%d", type, name, &pr, &qa);
// cout<<type<<' '<<name<<' '<<pr<<' '<<qa<<endl;
idx = idc(type);
pp[idx].pb({pr,qa});
r = max(r,qa);
}
l = 0;
while(l<=r){
int mid = l + (r-l+1)/2;//int mid = (l+r)>>1;
if(check(mid))ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n", ans);
}
return 0;
}