Chinaunix首页 | 论坛 | 博客
  • 博客访问: 469226
  • 博文数量: 117
  • 博客积分: 3195
  • 博客等级: 中校
  • 技术积分: 1156
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-04 01:44
文章分类

全部博文(117)

文章存档

2012年(5)

2011年(5)

2010年(46)

2009年(61)

我的朋友

分类:

2009-08-26 21:15:22

  

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

解题思路

题意:

要购买一套通讯系统,有一些设备组成,每个设备我们从一些制造商那里购得,带宽(bandwidth)是我们所选择的设备里面最小的带宽,总价格是我们选择的设备的总价格,我们的目标是求出最大的带宽和总价格的比值(B/P)。

 

思路:

   一开始我就想用贪心,每种都选出拥有最大(B/P)的设备,后来发现不行。后来参考了网上的思路,先枚举带宽,再贪心选择价格最小的,最后求出结果。

具体是在输入时就找出所有这些设备中最小的带宽(low)和最大带宽(high)。然后从low递增到high,每个制造商里都选价格最小的设备,这样就能保证B/P最小了。其实还能优化,把每个带宽都记录下来枚举,这样在带宽大小相差较大的情况下可以省下很多时间,好像要用到STL里的set,暂时还不会,以后再说。

源程序

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define N 101

int main()
{
    int i, j;
    int t, n, m[N], k;
    int band[N][N], price[N][N], low, high, sump, minp;
    float ratio;
    freopen("in.txt", "r", stdin);
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        low = 0xffff;
        high = 0;
        for(i=1; i<=n; i++)
        {
            scanf("%d", &m[i]);
            for(j=1; j<=m[i]; j++)
            {
                scanf("%d%d", &band[i][j], &price[i][j]);
                if(band[i][j] < low)
                    low = band[i][j];
                if(band[i][j] > high )
                    high = band[i][j];
            }
        }
        ratio = 0.0;
        for(i=low; i<=high; i++) //枚举band

        {
            sump = 0;
            for(j=1; j<=n; j++)
            {
                minp = 0xffff;
                for(k=1; k<=m[j]; k++) //贪心选择price

                    if(band[j][k] >= i && price[j][k] < minp)
                    {
                        minp = price[j][k];
                    }
                sump += minp;

            }

            if(i * 1.0 / sump - ratio > 0)
                ratio = i * 1.0 / sump;
        }
        printf("%.3f\n", ratio);
    }
    getch();
    return 0;
}

阅读(4451) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~