1018 Communication System - 贪心

题意:给出n种设备,每种有mi的制造商,每种设备有带宽和价钱两种属性,同种设备,不同制造商,这两种属性可能都不同。求一个选择制造商的方案,使带宽最小值(B)比总价钱(P)值最大。

按带宽求解,对于每个带宽值,求一个总价钱最小的方案。

代码如下:

  1. #include  <iostream>
  2. #include  <vector>
  3. using  namespace  std;
  4. typedef  struct  device
  5. {
  6.     vector <int>  bands;
  7.     vector <int>  prices;
  8.     
  9.     int  length()
  10.     {
  11.         return  bands.size();
  12.     }  
  13.     
  14.     void  clear()
  15.     {
  16.         bands.clear();
  17.         prices.clear();
  18.     }  
  19.     
  20.     void  add(int tmp_band, int tmp_price)
  21.     {
  22.         bands.push_back(tmp_band);
  23.         prices.push_back(tmp_price);
  24.     }    
  25. }decive;    
  26. int  main()
  27. {
  28.     int     t, n, i, mi;
  29.     int     band_max, band_min;
  30.     long    sum;
  31.     device  devices[100];
  32.     int     k, j, tmp_band, tmp_price;
  33.     double  max_bdivp;
  34.     
  35.     cin >> t;
  36.     
  37.     while (t--)
  38.     {
  39.         cin >> n;
  40.         
  41.         band_max = 0;
  42.         band_min = 32767;
  43.         
  44.         // input 
  45.         for (i = 0; i < n; ++i)
  46.         {
  47.             cin >> mi;
  48.             
  49.             devices[i].clear();         
  50.             
  51.             for (k = 0; k < mi; ++k)
  52.             {
  53.                 scanf("%d %d", &tmp_band, &tmp_price);
  54.                 
  55.                 devices[i].add(tmp_band, tmp_price);
  56.                 
  57.                 if (band_max < tmp_band)
  58.                 {
  59.                     band_max = tmp_band;
  60.                 }    
  61.                 
  62.                 if (band_min > tmp_band)
  63.                 {
  64.                     band_min = tmp_band;
  65.                 }    
  66.             }    
  67.         }
  68.         max_bdivp = 0.0;
  69.         // solve 
  70.         for(j = band_min; j <= band_max; ++j)
  71.         {
  72.             sum = 0;
  73.             
  74.             for(k = 0; k < n; ++k)
  75.             {
  76.                 int tmp = 32767;
  77.                 
  78.                 for(i = 0; i < devices[k].length(); i++)
  79.                 {
  80.                     if(devices[k].bands[i] >= j 
  81.                        && devices[k].prices[i]< tmp)
  82.                     {
  83.                         tmp = devices[k].prices[i];
  84.                     }    
  85.                 }    
  86.                 
  87.                 sum += tmp;
  88.              }
  89.              
  90.              if( j / (sum + 0.0) > max_bdivp)
  91.              {
  92.                  max_bdivp = j / (sum + 0.0);
  93.              }    
  94.          }
  95.          // output
  96.          printf("%.3lf/n", max_bdivp);
  97.     }
  98.     
  99.     return  0;
  100. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值