Assemble
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 628 Accepted Submission(s): 230
Problem Description
Recently your team noticed that the computer you use to practice for programming contests is not good enough anymore. Therefore, you decide to buy a new computer.
To make the ideal computer for your needs, you decide to buy separate components and assemble the computer yourself. You need to buy exactly one of each type of component.
The problem is which components to buy. As you all know, the quality of a computer is equal to the quality of its weakest component. Therefore, you want to maximize the quality of the component with the lowest quality, while not exceeding your budget.
Input
On 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.
Output
Per testcase:
One line with one integer: the maximal possible quality.
Sample Input
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 Output
如果单纯算二分的话,此题很水,于是作者在输入上做文章。。。
题意:
有n个零件,每个零件有“类型,名字,价格,质量”
要求在总价格不超过b的前提下,每种类型的零件买一个,使质量最大
其中,所谓质量最大是指使所有零件中质量最小的零件满足最大
分析:
二分枚举质量,检查该质量是否满足总价格不超过b
检查:在该类型的所有零件中,从质量大于mid的零件中找出价格最小的(质量大于mid保证mid是最小的质量)
二分完了就是输入,我是用不定长数组分别保存每种类型的价格和质量
具体操作见代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<cctype>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int N = 1000;
const int inf = 1e9+9;
int n,b;
struct node //一个零件的所以元素
{
char type[30];
char name[30];
int p,q;
}a[N+5];
bool cmp(node a,node b)//为了把类型相同的放到一起
{
if (strcmp(a.type,b.type) == 0)//类型相同的按质量从大到小排序
{
if (a.q == b.q) return a.p < b.p;//质量等的按价格从小到大排序
return a.q>b.q;
}
return strcmp(a.type,b.type)<0;//
}
int ol[N+5];//记录所有的质量,然后从小到大排序用于二分
struct Node
{
int p,q;
};
vector<Node>u[N+5];//不定长数组记录每种类型的零件的价格和质量
int k;//记录零件总类型数
bool ok(int mid) //二分的检查函数
{
ll sun = 0;
for (int i = 0;i < k;++i)
{
int mn = inf;//在满足的质量中找最小的价格
for (int j = 0;j < u[i].size();++j)
{
if (u[i][j].q>=mid) mn = min(mn,u[i][j].p);
else break;
}
sun += mn;
if (sun > b) return 0;
}
return 1;
}
int main()
{
int T;scanf("%d",&T);
while (T--)
{
scanf("%d %d",&n,&b);
for (int i = 0;i < n;++i)
{
u[i].clear();
scanf("%s %s %d %d",a[i].type,a[i].name,&a[i].p,&a[i].q);
ol[i] = a[i].q;
}
sort(ol,ol+n);
sort(a,a+n,cmp);
k = 0;
for (int i = 0;i+1 < n;++i)//将同种类型的存到一起,方便后面二分的检查
{
while (i+1<n&&strcmp(a[i].type,a[i+1].type)==0)
{
Node tmp;tmp.p = a[i].p,tmp.q=a[i].q;
u[k].push_back(tmp);
i++;
}
Node tmp;tmp.p = a[i].p;tmp.q = a[i].q;
u[k].push_back(tmp);
k++;
}
int l = 0,r = n-1;
int mid = (l+r)>>1;
while (l <= r)//二分
{
if (ok(ol[mid])) l = mid +1;
else r = mid - 1;
mid = (l+r)>>1;
}
printf("%d\n",ol[mid]);
}
return 0;
}