Communication System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29741 | Accepted: 10577 |
Description
We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we are free to choose from several manufacturers. Same devices from two manufacturers differ in their maximum bandwidths and prices.
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen devices in the communication system and the total price (P) is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P.
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen devices in the communication system and the total price (P) is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case starts with a line containing a single integer n (1 ≤ n ≤ 100), the number of devices in the communication system, followed by n lines in the following format: the i-th line (1 ≤ i ≤ n) starts with mi (1 ≤ mi ≤ 100), the number of manufacturers for the i-th device, followed by mi pairs of positive integers in the same line, each indicating the bandwidth and the price of the device respectively, corresponding to a manufacturer.
Output
Your program should produce a single line for each test case containing a single number which is the maximum possible B/P for the test case. Round the numbers in the output to 3 digits after decimal point.
Sample Input
1 3 3 100 25 150 35 80 25 2 120 80 155 40 2 100 100 120 110
Sample Output
0.649
这道题题意是有几种不同的设备,每种设备有多个厂家可以生产。B是所有设备的最小带宽,P是所有设备的价格总和,求B/P的最大值。因为两者相互关联,并且B要尽量大,P要尽量小。所以我们考虑先把B给固定下来,即先枚举一个B,然后求B固定的情况下,B/P的最大值。然后改变B的值,最后得出最后结果。
枚举时候可以将所有的设备信息保存到一个结构体数组里面,按照B升序,B相同时P升序,都相同时num升序这样排序。然后从前往后枚举即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<iomanip>
using namespace std;
struct node
{
int b;
int p;
int num;
}s[10010];
bool cnt(node a, node b)
{
if (a.b == b.b&&a.p == b.p)
return a.num < b.num;
if (a.b == b.b)
return a.p < b.p;
return a.b < b.b;
}
int main()
{
int T;
int n, m;
bool pos[111];
cin >> T;
while (T--)
{
cin >> n;
int cmp = 0;
for (int i = 0; i < n; i++)
{
cin >> m;
while (m--)
{
cin >> s[cmp].b >> s[cmp].p;
s[cmp].num = i;
cmp++;
}
}
sort(s, s + cmp, cnt);
double res = 0;
for (int i = 0; i < cmp - n + 1; i++)
{
double resb = s[i].b;
double resp = 0;
int count = 0;
memset(pos, 0, sizeof(pos));
for (int j = i ; j < cmp; j++)
{
if (!pos[s[j].num])
{
pos[s[j].num] = 1;
resp += s[j].p;
count++;
}
}
if (count == n)
res = max(res, resb / resp);
}
cout << fixed << setprecision(3) << res << endl;
}
return 0;
}