• POJ1018 Communication System


    题目来源:http://poj.org/problem?id=1018

    题目大意:

      某通讯公司想做一个特殊的通讯系统。该系统由若干个设备组成。每个设备我们可以自由选择不同的生产厂商。每个厂商生产的同一种设备的价格和带宽不同。系统的总带宽(B)是选择的所有设备的最小带宽,总价格(P)是所有设备的价格之和。目标是为每个设备选择一个厂商,使得总的B/P最大。

    输入:第一行一个整数代表测试用例个数。每个用例输入的第一个整数为设备数n(1<=n<=100),接下来n行,第i行的第一个整数m表示有多少个厂家生存第i中设备,后面的m对数据分别是每个厂家提供的第i种设备的带宽和价格。

    输出:每个用例一行,一个三位小数表示最大的B/P值。


    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)的可能值。显然有B不小于所有厂家提供的所有设备的最小值,不高于每种设备带宽最大值里的最小值。由此确定了B的取值范围后,再求每种可能的B值对应的最小的P,找出比值最大的即可。

      1 //////////////////////////////////////////////////////////////////////////
      2 //        POJ1018    Communication System
      3 //        Memory: 364K        Time: 63MS
      4 //        Language: C++        Result: Accepted
      5 //////////////////////////////////////////////////////////////////////////
      6 
      7 #include <iostream>
      8 #include <stdlib.h>
      9 
     10 using namespace std;
     11 
     12 //带宽、价格对
     13 class BV {
     14 public:
     15     int b, v;
     16     BV() {
     17         b = 0;
     18         v = 0;
     19     }
     20 };
     21 //设备各厂商的信息记录
     22 class DEV {
     23 public:
     24     BV bv[100];
     25     int bvCount;
     26     int maxB;
     27     int minB;
     28     DEV(){
     29         bvCount = 0;
     30         maxB = 0;
     31         minB = 2147483647;
     32     }
     33 };
     34 //存储数据
     35 DEV data[100];
     36 
     37 //设备数
     38 int d;
     39 int lowBound = 2147483647;
     40 int upBound = 2147483647;
     41 
     42 int compare(const void * a, const void * b) {
     43     return (*(BV*)a).b - (*(BV*)b).b;
     44 }
     45 
     46 int calculate(int b) {
     47     int v = 0;
     48     bool flag = false;
     49     for (int id = 0; id < d; ++id) {
     50         int tv = 2147483647;
     51         for (int im = 0; im < data[id].bvCount; ++im) {
     52             if (data[id].bv[im].b < b) {
     53                 continue;
     54             } else if (data[id].bv[im].b == b) { 
     55                 flag = true;
     56             }
     57             if (data[id].bv[im].v < tv) {
     58                 tv = data[id].bv[im].v;
     59             }
     60         }
     61         v += tv;
     62     }
     63     if (flag == true) {
     64         return v;
     65     } else return 0;
     66 }
     67 
     68 int main() {
     69     int n;
     70     cin >> n;
     71     for (int caseNo = 0; caseNo < n; ++caseNo) {
     72         //设备数
     73         cin >> d;    
     74         //读取输入 计算bandwidth的可能范围
     75         for (int id = 0; id < d; ++id) {
     76             cin >> data[id].bvCount;
     77             for (int im = 0; im < data[id].bvCount; ++im) {
     78                 cin >> data[id].bv[im].b >> data[id].bv[im].v;
     79             }
     80             qsort(data[id].bv, data[id].bvCount, sizeof(BV), compare);
     81             data[id].maxB = data[id].bv[data[id].bvCount - 1].b;
     82             data[id].minB = data[id].bv[0].b;
     83         }
     84         lowBound = 2147483647;
     85         upBound = 2147483647;
     86         for (int id = 0; id < d; ++id) {
     87             if (data[id].minB < lowBound) {
     88                 lowBound = data[id].minB;
     89             }
     90             if (data[id].maxB < upBound) {
     91                 upBound = data[id].maxB;
     92             }
     93         }
     94         float maxbvr = 0;
     95         for (int b = lowBound; b <= upBound; ++b) {
     96             int v = calculate(b);
     97             if (v != 0) {
     98                 double r = (double)b / v;
     99                 if (r > maxbvr) {
    100                     maxbvr = r;
    101                 }
    102             }
    103         }
    104         cout.setf(ios::showpoint);
    105         cout.precision(3);
    106         cout.setf(ios::fixed); 
    107         cout << maxbvr << endl;
    108     }
    109     system("pause");
    110     return 0;
    111 }
    View Code
  • 相关阅读:
    Java——class对象
    Java——HashSet和TreeSet的区别
    Java——普通数据类型转化为String
    Java——Iterator迭代器
    Java——ArrayList和LinkedList的区别(转)
    Java——警告消除
    Object类——toString
    Java——动态调用类中方法
    SharePoint使用jsom查询当前用户信息
    SharePoint列表模板(.stp)
  • 原文地址:https://www.cnblogs.com/dengeven/p/3227936.html
Copyright © 2020-2023  润新知