直接二分搜品质因子
AC代码如下:
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define MAX 0x3f3f3f3f
int N, B;
map<string,int> id;
vector<int> price[1100], para[1100];
int cnt, L, R;
bool judge( int M ){
int sum = 0;
for( int i = 1; i <= cnt; i++ ){
int temp = MAX;
for( int j = 0; j < price[i].size(); j++ ){
if( para[i][j] >= M ){
temp = min( temp, price[i][j] );
}
}
sum += temp;
if( sum > B ){
return false;
}
}
return true;
}
int main(){
int T;
scanf( "%d", &T );
while( T-- ){
scanf( "%d%d", &N, &B );
id.clear();
for( int i = 0; i < 1100; i++ ){
price[i].clear();
para[i].clear();
}
cnt = 0;
R = 0;
for( int i = 0; i < N; i++ ){
string temp1, temp2;
int temp3, temp4;
cin >> temp1 >> temp2 >> temp3 >> temp4;
R = max( R, temp4 );
if( id.find( temp1 ) != id.end() ){
price[id[temp1]].push_back( temp3 );
para[id[temp1]].push_back( temp4 );
}else{
id[temp1] = ++cnt;
price[id[temp1]].push_back( temp3 );
para[id[temp1]].push_back( temp4 );
}
}
L = 0;
while( L < R ){
int M = L + ( R - L + 1 ) / 2;
if( judge( M ) ){
L = M;
}else{
R = M - 1;
}
}
cout << L << endl;
}
return 0;
}