InputOn the first line one positive number: the number of testcases, at most 100. After that per testcase:
One line with two integers: 1 ≤ n ≤ 1 000, the number of available components and 1 ≤ b ≤ 1 000 000 000, your budget.
n lines in the following format: 「type name price quality」, where type is a string with the type of the component, name is a string with the unique name of the component, price is an integer (0 ≤ price ≤ 1 000 000) which represents the price of the component and quality is an integer (0 ≤ quality ≤ 1 000 000 000) which represents the quality of the component (higher is better). The strings contain only letters, digits and underscores and have a maximal length of 20 characters.
OutputPer testcase:
One line with one integer: the maximal possible quality.Sample Inputnode
1 18 800 processor 3500_MHz 66 5 processor 4200_MHz 103 7 processor 5000_MHz 156 9 processor 6000_MHz 219 12 memory 1_GB 35 3 memory 2_GB 88 6 memory 4_GB 170 12 mainbord all_onboard 52 10 harddisk 250_GB 54 10 harddisk 500_FB 99 12 casing midi 36 10 monitor 17_inch 157 5 monitor 19_inch 175 7 monitor 20_inch 210 9 monitor 22_inch 293 12 mouse cordless_optical 18 12 mouse microsoft 30 9 keyboard office 4 10
Sample Outputios
9
1 /* 2 解題思路: 3 1. 求解這類"最小值最大"的問題通常方法"二分法". 肯定最大和最小的quality, 經過二分 4 枚舉, 判斷是否知足最小价錢不超過b價格. 問題天然求解. 5 2. poj這題數據有點BT, 好屢次TLE, 合理使用STL才行. 用map查找配件類型, 875MS時間 6 不高, 後來網上發現根據quality排序能夠在判斷是否知足時候提速, 好方法. 7 */ 8 #include <cstdio> 9 #include <iostream> 10 #include <cstring> 11 #include <string> 12 #include <vector> 13 #include <map> 14 using namespace std; 15 #define MAX 1005 16 const int INF = (1<<29); 17 18 struct node{ 19 int price, qu; 20 }good[MAX][MAX]; 21 22 int n, b; 23 map pos; 24 int count[MAX]; 25 int num; 26 27 inline int max(int a, int b){ 28 return a > b ? a : b; 29 } 30 31 inline int min(int a, int b){ 32 return a < b ? a : b; 33 } 34 35 inline int find(string str){ 36 if( !pos.count(str) ) pos[str] = num++; 37 return pos[str]; 38 } 39 40 inline bool judge(int qu){ 41 int result = 0; 42 for(int i = 0; i < num; ++i){ 43 int minOne = b+1; 44 for(int j = 0; j < count[i]; ++j){ 45 if( good[i][j].qu >= qu ) 46 minOne = min(minOne, good[i][j].price); 47 } 48 result += minOne; 49 if(result > b) return false; 50 } 51 return true; 52 } 53 54 int main(){ 55 int caseNum; 56 scanf("%d", &caseNum); 57 while(caseNum--){ 58 scanf("%d %d", &n, &b); 59 num = 0; 60 61 memset(count, 0, sizeof(count)); 62 pos.clear(); 63 int left = INF, right = 0; 64 int price, qu; 65 char name[22], type[22]; 66 for(int i = 0; i < n; ++i){ 67 scanf("%s %s %d %d", type, name, &price, &qu); 68 right = max(right, qu); 69 left = min(left, qu); 70 int temp = find(type); 71 good[temp][count[temp]].price = price; 72 good[temp][count[temp]++].qu = qu; 73 } 74 75 while(left <= right){ 76 int mid = (left+right)>>1; 77 if( judge(mid) ) left = mid+1; 78 else right = mid-1; 79 } 80 printf("%d\n", right); 81 } 82 return 0; 83 }