Communication System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23051 | Accepted: 8191 |
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
题意:
要购买一套通讯系统,有一些设备组成,每个设备我们从一些制造商那里购得,带宽(bandwidth)是我们所选择的设备里面最小的带宽,总价格是我们选择的设备的总价格,我们的目标是求出最大的带宽和总价格的比值(B/P)。
思路:
一开始我就想用贪心,每种都选出拥有最大(B/P)的设备,后来发现不行。后来参考了网上的思路,先枚举带宽,再贪心选择价格最小的,最后求出结果。
具体是在输入时就找出所有这些设备中最小的带宽(low)和最大带宽(high)。然后从low递增到high,每个制造商里都选价格最小的设备,这样就能保证B/P最小了。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
struct node
{
int b;
int p;
};
node a[101][101];
int main()
{
int b[101];
int n,m,Min,sum;
int low,high;
cin>>n;
while (n--)
{
cin>>m; //公司数量
low=100000;
high=-1;
for (int i=0; i<m; i++)
{
cin>>b[i]; //设备数量
for (int j=0; j<b[i]; j++)
{
cin>>a[i][j].b>>a[i][j].p;
if(a[i][j].b < low)
{
low = a[i][j].b;
}
if(a[i][j].b > high )
{
high = a[i][j].b;
}
}
}
double result=-1;
for (int i=low; i<=high;i++) //从最小到最大开始找
{
sum=0;
for (int j=0; j<m; j++) //在每个公司里面选
{
Min=100000000;
for (int k=0; k<b[j]; k++) //在每个公司里面选择p最小的
{
if(a[j][k].b>=i &&a[j][k].p<Min)
{
Min=a[j][k].p;
}
}
sum+=Min; //各个公司中选出来的p之和
}
if(i*1.0/sum>result)
{
result=i*1.0/sum;
}
}
printf("%.3lf\n",result);
}
return 0;
}