Communication System题解

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 § 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
这个题做的时候就不是很明白,去找了好多分代码,也怪自己英语太差。。
从set中的第一个宽带也就是最小的开始,每个厂商若存在宽带比他大且价格最小的则选其一加到sum上,若没有且其本身不属于该组就跳,其本身在就自加,用本身除以现在的sum得到b/q,感觉差点绕死。。。
三重for循环,第一重用于set中寸的宽带依次比较,第二重为厂家,第三重为个个厂家中的各组宽带

#include <stdio.h>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std; 
set <int> s; //用以储存宽带
set <int> ::iterator it;//借助迭代器进行查找
 int band[105][105], price[105][105], m[105];//用以储存宽带,和相应的价格
 int main() {
    int T,n,i,j;
    cin>>T;
    while(T--){
        cin>>n;
        s.clear();
        for(i=0;i<n;i++){
            cin>>m[i];
            for(j=0;j<m[i];j++){
                cin>>band[i][j]>>price[i][j];
                s.insert(band[i][j]);
            }
        }
        double ans=0;
        for(it=s.begin();it!=s.end();it++){
            int k=*it,sum_price=0;
            for(i=0;i<n;i++){
                int min_price=0x3fffff;//16进制的最大值
                for(j=0;j<m[i];j++){
                    if(band[i][j]>=k&&price[i][j]<min_price)
                        min_price=price[i][j];//如果当前的宽带相比最小的那个性价比更高,则它变为性价最高者
                }
                sum_price+=min_price;
            }
            if(k*1.0/sum_price>ans)
                ans=k*1.0/sum_price;
        }
        printf("%.3f\n",ans);
    }
    return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_43141958/article/details/88740740
今日推荐