题意
请参考LRJ白书第一章例题11
思路
二分答案。
典型的“最小值最大问题”,考虑二分答案,检查一次二分的时间是可以接受的,因此本题得到解决。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <ctime> #include <iomanip> #include <cmath> #include <set> #include <stack> #include <cmath> #include <map> #include <complex> using namespace std; struct Component{ int q,p; bool operator < (const Component &b) const{ if(q == b.q){ return p < b.p; } return q < b.q; } Component(){ } Component(int _p,int _q){ p = _p; q = _q; } }; typedef map<string,vector<Component> > Table; Table tab; int n,b; void input(){ tab.clear(); scanf(" %d %d",&n,&b); for(int i = 1; i <= n; i++){ string type; string name; int p,q; cin >> type >> name >> p >> q; tab[type].push_back(Component(p,q)); } for(Table::iterator it = tab.begin(); it != tab.end(); it++){ sort(it->second.begin(),it->second.end()); } } bool check(int miq){ int cst = 0; for(Table::iterator it = tab.begin(); it != tab.end(); it++){ int sel = b+1; for(int i = 0; i < it->second.size(); i++){ if(it->second[i].q >= miq){ sel=min(sel,it->second[i].p); } } if(sel == b+1){ return false; }else{ cst += sel; if(cst > b){ return false; } } } return true; } int solve(){ int l = 0,r = 1000000000,mid; while(l < r){ mid = l + (r-l+1)/2; if(check(mid)){ l = mid; }else{ r = mid-1; } } if(check(r)){ return r; } else{ return l; } } int main(){ int caseCnt; scanf(" %d",&caseCnt); while(caseCnt--){ input(); cout << solve() << endl; } return 0; } |