题意: 给定电脑每个配件的 价格,名称, 品质因子
在不超出总价的情况下,每种配件都要选择,然后使得“最小的品质因子最大”
把配件存入一个数组,一次按照 名称, 价格从小到大, 品质因子从大到小排序
用一个set 记录是否选择了某一种配件
把所有品质因子存入数组,从小到大排序
二分查找,能够满足 则ans >= 当前值 否则 ans < 当前值
重载小于 < 为成员函数 STL sort通不过
然后 ans 没初始化 WA了一次
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;
///宏定义
const int INF = 20000000;
const int maxn = 1080;
///全局变量 和 函数
//
int T;
int n, b;
int cnts;
struct things
{
string name;
int price;
int quality;
};
bool operator < (const things& t1, const things& t2)
{
if (t1.name != t2.name)
{
return t1.name < t2.name;
}
else
{
if (t1.price != t2.price)
{
return t1.price < t2.price;
}
else
return t1.quality > t2.quality;
}
}
things Things[maxn];
set<string> vis;
int q[maxn];
bool test(int qua)
{
int i, j;
int prices = 0;
int cnt = 0;
vis.clear();
for (i = 0; i < n; i++)
{
if (vis.count(Things[i].name) == 0 && Things[i].quality >= qua)
{
prices += Things[i].price;
vis.insert(Things[i].name);
cnt++;
}
}
if (cnt < cnts || prices > b)
{
return false;
}
return true;
}
int main()
{
//
int i, j;
cin >> T;
while (T--)
{
vis.clear();
cin >> n >> b;
int low = 0, upp = n - 1;
cnts = 0;
for (i = 0; i < n; i++)
{
string tmp;
string name;
int price, quality;
cin >> name >> tmp >> price >> quality;
Things[i].name = name;
Things[i].price = price;
Things[i].quality = quality;
q[i] = quality;
if (vis.count(name) == 0)
{
vis.insert(name);
cnts++;
}
}
sort(Things, Things + n);
sort(q, q + n);
int ansl = 0;
int ansh = n - 1;
while (low <= upp)
{
int mid = (low + upp) / 2;
if (test(q[mid]))
{
low = mid + 1;
ansl = max(ansl, mid);
}
else
{
upp = mid - 1;
ansh = min(ansh, mid);
}
}
int ans = q[ansl]; //问题出在这里。。
for (i = ansl; i < ansh; i++)
{
ans = q[i];
}
cout << ans << endl;
}
return 0;
}