时间:2016-03-19 13:55:17 星期六
题目编号:[2016-03-19][UVALive][3971][Assemble]
题目大意:给定若干个电脑零件的价格和质量,求在总价不超过b的情况下,品质最差的配件的质量尽可能大
分析:二分
#include <vector>#include <map>#include <algorithm>#include <string>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;#define CLR(x,y) memset((x),(y),sizeof((x)))#define CLR2(x,y,mtype,mcnt) memset((x),(y),sizeof((mtype))*(mcnt))#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);--(x))const int maxn = 1000 + 100;struct Com{ int price,quality; Com(int p = 0,int q = 0):price(p),quality(q){} };vector<Com> comp[maxn];map<string,int> id;int t,n,b,p,q,maxq,cnt;int ID(string str){ if(id.count(str)) return id[str]; else id[str] = cnt; return cnt++;}int ok(int q){ int sum = 0; FOR(i,0,cnt){ int chepest = b + 1; int m = comp[i].size(); FOR(j,0,m){ if(comp[i][j].quality >= q && comp[i][j].price < chepest) chepest = comp[i][j].price; } sum += chepest; if(sum > b) return 0; } return 1;}int main(){ char type[30]; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&b); cnt = 0; FOR(i,0,n) comp[i].clear(); id.clear(); maxq = 0; FOR(i,0,n){ scanf("%s %*s %d %d",type,&p,&q); if(q > maxq) maxq = q; comp[ID(type)].push_back( Com(p,q) ); } int l = 0,r = maxq,mid; while(l < r){ mid = l + (r - l + 1)/2; if(ok(mid)) l = mid; else r = mid - 1; } printf("%d\n",l); } return 0;}