码迷,mamicode.com
首页 > 其他好文 > 详细

LA3971 组装电脑

时间:2017-04-12 23:34:30      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:bool   ice   技术   color   while   include   else   nbsp   return   

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1972

题意:

有 b 块钱, n个配件,配件有 种类,名字,价格,和品质。要求每一类都要有,价格总和不能超过 b,最后要求最低的品质的那个配件的品质要最高。

 

分析:

二分。

二分品质x,低于 x 的筛掉,高于等于 x 的,选一个最便宜的。

处理好每种配件,插到vector中,输入麻烦一点。

技术分享
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int maxn = 1000 + 5;
 6 const int inf = 0x3f3f3f3f;
 7 int n,b;
 8 
 9 struct Component {
10     int price;
11     int quality;
12 };
13 
14 vector<Component> comp[maxn];
15 
16 int cnt;
17 map<string,int> id;
18 int ID(string s) {
19     if(!id.count(s))
20         id[s] = cnt++;
21     return id[s];
22 }
23 
24 bool ok(int q)
25 {
26     int sum = 0;
27     for(int i=0;i<cnt;i++) {
28         int m = comp[i].size();
29         int cheap = inf;
30         for(int j=0;j<m;j++) {
31             if(comp[i][j].quality>=q)
32                 cheap = min(cheap,comp[i][j].price);
33         }
34         if(cheap==inf) return false;
35         sum+=cheap;
36         if(sum>b) return false;
37     }
38     return true;
39 }
40 
41 int main()
42 {
43     int t;
44     scanf("%d",&t);
45     while(t--) {
46         cnt = 0;
47         int maxq = 0;
48         scanf("%d%d",&n,&b);
49         for(int i=0;i<n;i++) {
50             comp[i].clear();
51         }
52 
53         for(int i=0;i<n;i++) {
54             char type[30],name[30];
55             int p,q;
56             scanf("%s%s%d%d",type,name,&p,&q);
57             maxq = max(maxq,q);
58             comp[ID(type)].push_back((Component){p,q});
59         }
60 
61         int L = 0,R=maxq+1;
62         while(L<R) {
63             int M = L + (R-L+1)/2;
64             if(ok(M))
65                 L = M;
66             else R = M - 1;
67         }
68         printf("%d\n",L);
69     }
70     return 0;
71 }
View Code

 

LA3971 组装电脑

标签:bool   ice   技术   color   while   include   else   nbsp   return   

原文地址:http://www.cnblogs.com/TreeDream/p/6701591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!