zoukankan      html  css  js  c++  java
  • Uva 组装电脑 12124

    主要运用二分法查找最优解

     1 #include<iostream>
     2 #include<string>
     3 #include<vector>
     4 #include<map>
     5 using namespace std;
     6 
     7 const int maxn=1000+5;
     8 
     9 int T,n,b,cnt;
    10 
    11 map<string,int> id;
    12 int ID(string s)
    13 {
    14     if(!id.count(s)) id[s]=cnt++;
    15     return id[s];
    16 }
    17 
    18 struct Component
    19 {
    20     int price;
    21     int quality;
    22 };
    23 vector<Component> comp[maxn]; 
    24 
    25 bool ok(int q)
    26 {
    27     int sum=0;
    28     for(int i=0;i<cnt;i++)
    29     {
    30         int cheapest=b+1,m=comp[i].size();
    31         for(int j=0;j<m;j++)
    32             if(comp[i][j].quality>=q) 
    33                 cheapest=min(cheapest,comp[i][j].price);
    34         if(cheapest==b+1) return false;
    35         sum+=cheapest;
    36         if(sum>b) return false;
    37     }
    38     return true;
    39 }
    40 
    41 int main()
    42 {
    43     cin>>T;
    44     for(int k=1;k<=T;k++)
    45     {
    46         cin>>n>>b;
    47         cnt=0;
    48         for(int i=0;i<n;i++) comp[i].clear();
    49         id.clear();
    50         int maxq=0;
    51         for(int i=0;i<n;i++)
    52         {
    53             string type,name;
    54             int p,q;
    55             cin>>type>>name>>p>>q;
    56             maxq=max(maxq,q);
    57             comp[ID(type)].push_back((Component){p,q});
    58         }
    59         int L=0,R=maxq;
    60         while(L<R)
    61         {
    62             int M=(L+R+1)/2;
    63             if(ok(M)) L=M;else R=M-1;
    64         }
    65         cout<<L<<endl;
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    用servlet来实现验证码的功能
    Sqlite3 数据库
    xml解析
    Android .9文件
    AsyncTask
    Looper Handler
    URLConnection
    单例模式
    Httpclient访问网络
    json 解析
  • 原文地址:https://www.cnblogs.com/InWILL/p/5552780.html
Copyright © 2011-2022 走看看