uva 12124 —— Assemble
题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42087
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <vector> 5 #include <map> 6 #include <algorithm> 7 #define MAXN 0x3f3f3f3f 8 using namespace std; 9 10 struct P { 11 int price, quality; 12 bool operator<(const P& p) const { 13 if(quality == p.quality) return price < p.price; 14 return quality < p.quality; 15 } 16 }; 17 18 19 vector<P> C[1005]; 20 int tot, T, n, b; 21 // O(logn)复杂度的检测 22 bool test(int x) 23 { 24 int sum = 0; 25 for(int i=0; i<tot; i++) { 26 int lb = -1, ub = C[i].size()-1; 27 while(ub - lb > 1) { // ( , ] 28 int m = (ub + lb) >> 1; 29 if(C[i][m].quality >= x) ub = m; 30 else lb = m; 31 } 32 if(C[i][ub].quality < x) return false; 33 int temp = MAXN; 34 for(int j=ub; j<C[i].size(); j++) { 35 temp = min(temp, C[i][j].price); 36 } 37 sum += temp; 38 if(sum > b) return false; 39 } 40 return true; 41 } 42 // O(n)复杂度的检测 43 bool test2(int x) 44 { 45 int cost, sum=0; 46 for(int i=0; i<tot; i++) 47 { 48 cost = MAXN; 49 for(int j=0; j<C[i].size(); j++) 50 { 51 if(C[i][j].quality >= x) 52 cost = min(cost, C[i][j].price); 53 } 54 if(cost==MAXN) return 0; 55 sum += cost; 56 } 57 return sum <= b; 58 } 59 60 int main () 61 { 62 string type, name; 63 scanf("%d", &T); 64 while(T--) { 65 scanf("%d%d", &n, &b); 66 int cur = 0, lb, ub; 67 tot = 0; 68 map<string, int> mp; 69 for(int i=0; i<n; i++) { 70 P p; 71 cin >> type >> name >> p.price >> p.quality; 72 if(i == 0 || p.quality > ub) ub = p.quality; 73 if(i == 0 || p.quality < lb) lb = p.quality; 74 75 if(mp.find(type) == mp.end()) { 76 cur = tot; 77 mp[type] = tot++; 78 } else { 79 cur = mp[type]; 80 } 81 C[cur].push_back(p); 82 } 83 for(int i=0; i<tot; i++) { 84 sort(C[i].begin(), C[i].end()); 85 } 86 ub++; 87 while(ub - lb > 1) { // [ , ) 88 int m = (lb + ub) >> 1; 89 90 if(test(m)) lb = m; 91 else ub = m; 92 } 93 printf("%d\n", lb); 94 for(int i=0; i<tot; i++) 95 C[i].clear(); 96 } 97 return 0; 98 }