首页 > 代码库 > 2017ecjtu-summer training #11 POJ 1018

2017ecjtu-summer training #11 POJ 1018

Communication System
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29218   Accepted: 10408

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

题意 气得死,该死的英语。t组测试,每次输入一个n(要买的设备总数),接下来n行每行第一个数为生产i(1<=i<=n)设备的厂家数mi,紧接着是每个厂家生产该设备的bandwidth, price;
B为所选的n个设备中bandwidth的最小值,P为所选的n个设备price的和,问你怎样才能使B/P最大,输出B/P保留三位小数。
解析 该题有很多解法,我选择了贪心,比较好想;
B的取值范围,最小值每个设备所有生产商bandwidth的最小值,最大值每个设备所有生产商bandwidth最大值的最小值(样例中就是 80<=B<=120)
枚举区间中的bandwidth,每次在每个设备所有生产商中找bandwidth>=B且price最小的,把price累加起来,最后计算出B/P与上一次枚举的结果进行比较,留下较大的那个。

AC代码
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <queue>
 8 #include <vector>
 9 #include <algorithm>
10 #define maxn 105
11 #define inf  0x3f3f3f3f
12 using namespace std;
13 typedef long long ll;
14 struct node
15 {
16     double b;
17     double p;
18 };
19 node f[maxn][maxn];
20 int main(int argc, char const *argv[])
21 {
22     int t,n;
23     int a[maxn];
24     double bz[maxn*maxn];
25     double b[maxn];
26     scanf("%d",&t);
27     while(t--)
28     {
29         scanf("%d",&n);
30         memset(a,0,sizeof(a));
31         memset(b,0,sizeof(b));
32         memset(bz,0,sizeof(bz));
33         int cnt1=0,cnt2=0;
34         for(int i=1;i<=n;i++)
35         {
36             scanf("%d",&a[i]);
37             double maxb=0;
38             for(int j=1;j<=a[i];j++)
39             {
40                 scanf("%lf %lf",&f[i][j].b,&f[i][j].p);
41                 bz[cnt1++]=f[i][j].b;          //把所有的b放到一个数组,之后再排序,方便枚举
42                 maxb=max(maxb,f[i][j].b);      //找出每个设备的b的最大值
43             }
44             b[cnt2++]=maxb;                      //存进数组排序,b[0]就是所有设备的b的最大值的最小值
45         }
46         sort(bz,bz+cnt1);
47         sort(b,b+cnt2);
48         double maxbp=0;
49         for(int i=0;bz[i]<=b[0];i++)              //枚举
50         {
51             double sump=0;
52             for(int j=1;j<=n;j++)                 //第j个设备
53             {
54                 double minp=inf;
55                 for(int k=1;k<=a[j];k++)           //第j个设备的第k个生产商
56                 {
57                     if(f[j][k].p<minp&&f[j][k].b>=bz[i])     //找出最符合条件的p值
58                     {
59                         minp=f[j][k].p;
60                     }
61                 }
62                 sump+=minp;
63             }
64             double ans=bz[i]/sump;
65             if(ans>maxbp)
66                 maxbp=ans;                      //每次比较,留下较大的
67         }
68         printf("%.3lf\n",maxbp);
69     }
70     return 0;
71 }

我比较菜,程序跑的慢,700+ms,QWQ

2017ecjtu-summer training #11 POJ 1018