Uvalive 3971
Test instructions: B money, want to assemble a computer, give the type of n accessories, name, price, quality factor. If all kinds of accessories buy one, the total price <=b, to find the worst quality accessories of the maximum quality factor.
Ideas:
The maximum minimum value is usually divided into two methods.
In (0,MAXQ), it is possible to determine whether Q is the worst quality factor in two points.
Big Baishou problem, compare Test Code Foundation.
Code
/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip>using namespace Std;const double eps (1e-8); typedef long LONG lint;const int MAXN = + 5;map< String,int> id;int N,b;int cnt = 0;struct comp{int price;int quality;}; Vector<comp> Comp[maxn];int ID (string s) {if (!id.count (s)) {id[s] = cnt++;} return id[s];} bool OK (int q) {int sum = 0;//cout << q << endl;for (int i = 0; i < cnt; i++) {int cheapest = B + 1;for (int j = 0; J < Comp[i].size (); J + +) {//cout << comp[i][j].price << endl;if (q <= comp[i][j].quality) cheapest = min (cheapest, COMP[I][J].P Susan);} if (cheapest = = b+1) return false;sum + = Cheapest;//cout << "Q:" << q << "sum:" << sum << ; Endl;if (sum > B) return false;} return true;} int solve (int l, int r) {while (L < r) {int m = L + (r-l + 1)/2;if (OK (m)) L = m;else r = m-1;//cout << m <& Lt Endl;} return l;} int main () {//freopen ("Input.txt", "R", stdin), int t;cin >> t;while (t--) {cnt = 0;cin >> n >> b;for (int i = 0; I < n; i++) comp[i].clear (); int maxq = 0;for (int i = 1; I <= n; i++) {string type,name;int p,q;cin >> type >> nam E >> p >> q;//cout << type << name << p << q << endl;maxq = max (MAXQ, q); Comp tmp;tmp.price = p; tmp.quality = Q;comp[id (type)].push_back (TMP);} cout << maxq << endl;int L = 0, R = maxq;int ans = solve (l,r); cout << ans << endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Uvalive 3971 Assemble (analog + binary)