#include <map>
#include <set>
#include <list>
#include <cmath>
#include<cctype>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b)
{
return a % b == 0 ? b : gcd(b, a % b);
}
#define MAXN 1005
struct node
{
int value;
int qlty;
friend bool operator < (const node &a,const node &b)
{
if (a.value==b.value) return a.qlty>b.qlty;
return a.value<b.value;
}
}src[MAXN][MAXN];
int N,B;
int cas;
int num[MAXN];
int limit;
map<string , int> m;
void read()
{
m.clear();
cas=1,limit=0;
memset(num,0,sizeof(num));
char str[30],tmp[30];
scanf("%d%d\n",&N,&B);
for (int i=0;i<N;i++)
{
int tv,tq;
scanf("%s",str);
if (!m[str]) m[str]=cas++;
scanf("%s%d%d",tmp,&tv,&tq);
if (tq>limit) limit=tq;
src[m[str]][num[m[str]]].qlty=tq;
src[m[str]][num[m[str]]++].value=tv;
}
}
bool judge(int mid)//´«µÝÖÊÁ¿Öµ
{
int sum=0,j;
for (int i=1;i<cas;i++)
{
for (j=0;j<num[i];j++)
{
if (src[i][j].qlty>=mid && sum+src[i][j].value<=B)
{ sum+=src[i][j].value;break;}
}
if (j==num[i]) return false;
}
return true;
}
int binary()
{
int mid,left=0,right=limit;
while (left<right)
{
//printf("%d %d\n",left,right);
mid=(left+right)/2;
if (left==mid) break;
if (judge(mid)) left=mid;
else right=mid;
}
//printf("%d %d %d\n",left,mid,right);
//if (judge(mid+1)) mid++;
return mid;
}
int main()
{
// freopen("sample.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
read();
//printf("%d %d\n",cas,limit);
for (int i=1;i<cas;i++) sort(src[i],src[i]+num[i]);
//for (int i=1;i<cas;i++) printf("%d ",num[i]);
int ans=binary();
printf("%d\n",ans);
}
return 0;
}
UVALIVE 3971 Assemble 二分+贪心
最新推荐文章于 2019-07-07 13:50:49 发布