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

时间: 2024-10-13 19:56:58

2017ecjtu-summer training #11 POJ 1018的相关文章

POJ 1018 Communication System 题解

本题一看似乎是递归回溯剪枝的方法,我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了很久,无奈忍不住偷看了下提示,发现方法真多,有贪心,DP,有高级剪枝的,还有三分法的,八仙过海各显神通啊. 坏习惯了,没思考够深入就偷看提示了. 幸好及时回头,还不需要看别人的代码了.自己做出来之后,有空看看多种解法的代码也好. 然后我想出自己的思路了,使用贪心,剪枝,DP综合优化下,呵呵,最后程序有点复杂,优化到了16ms,运气好点,或者vector换成原始数组的话,应该可以0MS了. 总体思

POJ 1018 Communication System (动态规划)

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22500   Accepted: 8008 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.

POJ 1018 &amp; HDU 1432 Lining Up 【简单几何】

Lining Up Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24786   Accepted: 7767 Description "How am I ever going to solve this problem?" said the pilot. Indeed, the pilot was not facing an easy task. She had to drop packages at spe

POJ 1018 Communication System(DP)

http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产的同种设备都会存在两个方面的差别:带宽bandwidths 和 价格prices. 现在每种设备都各需要1个,考虑到性价比问题,要求所挑选出来的n件设备,要使得B/P最大. 其中B为这n件设备的带宽的最小值,P为这n件设备的总价. 思路:DP解决. d[i][j]代表选择第i个设备时最小带宽j时的价

贪心/poj 1018 Communication System

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int INF=0xfffffff; 6 struct node 7 { 8 int b,p; 9 }; 10 node a[110][110]; 11 int m[110]; 12 int pmin,psum,bmax,n; 13 double ans; 14 int main() 15

poj 1018(dp)

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25653   Accepted: 9147 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.

2017ecjtu-summer training #7 POJ 2689

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18731   Accepted: 5006 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

ecjtu-summer training #11

A - Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchase

poj 1018 Communication System (枚举)

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted: 7953 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.