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.
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肯定
是当前最小带宽的最大值,然后统计最大的就行。
#include <stdio.h>
#include <vector>
#include <set>
#define INF 0x7fffffff
#define LL long long
const int maxn = 105;
using namespace std;
struct node
{
int b, p;
} t;
double ans;
int ok;
vector < node > mp[maxn];
set < int > vis;
void init ( )
{
for ( int i = 0; i < maxn; i ++ )
mp[i].clear ( ); //清空
vis.clear ( );
ans = 0;
}
inline int Min ( int a, int b )
{
return a < b ? a : b;
}
template < class T > //模板最大值
inline T Max ( T a, T b )
{
return a > b ? a : b;
}
int main ( )
{
int T, n, m, minp;
scanf ( "%d", &T );
while ( T -- )
{
init ( );
scanf ( "%d", &n );
for ( int i = 0; i < n; i ++ )
{
scanf ( "%d", &m );
for ( int j = 0; j < m; j ++ )
{
scanf ( "%d%d", &t.b, &t.p );
mp[i].push_back ( t );
if ( ! vis.count ( t.b ) ) //将每个的带宽加入set
vis.insert ( t.b );
}
}
set < int > :: iterator it; //迭代器
for ( it = vis.begin ( ); it != vis.end ( ); it ++ )
//b从小枚举到大,排序也可以
{
LL sum = 0;
for ( int i = 0; i < n; i ++ )
{
minp = INF; //找每组值最小的,没有就一个最大值就不会成立
int len = mp[i].size ( );
for ( int j = 0; j < len; j ++ )
if ( mp[i][j].b >= *it )
minp = Min ( minp, mp[i][j].p );
sum = sum+minp;
}
ans = Max ( ans, *it*1.0/sum ); //找每组p为最小,那么b/p肯定为最大的
}
printf ( "%.3lf\n", ans );
}
return 0;
}