uva 12124 —— Assemble

本文提供了一道ACM竞赛编程题的解决方案,采用C++实现。通过对商品价格和质量进行结构化存储,并利用二分查找法高效求解最优质量阈值,确保总花费不超过预算。代码中展示了如何定义结构体、自定义比较运算符、使用STL容器及算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42087

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <vector>
 5 #include <map>
 6 #include <algorithm>
 7 #define MAXN 0x3f3f3f3f
 8 using namespace std;
 9 
10 struct P {
11     int price, quality;
12     bool operator<(const P& p) const {
13         if(quality == p.quality)    return price < p.price;
14         return quality < p.quality;
15     }
16 };
17 
18 
19 vector<P> C[1005];
20 int tot, T, n, b;
21 // O(logn)复杂度的检测
22 bool test(int x) 
23 {
24     int sum = 0;
25     for(int i=0; i<tot; i++) {
26         int lb = -1, ub = C[i].size()-1;
27         while(ub - lb > 1) { // ( , ]
28             int m = (ub + lb) >> 1;
29             if(C[i][m].quality >= x)    ub = m;
30             else lb = m;
31         }
32         if(C[i][ub].quality < x)    return false;
33         int temp = MAXN;
34         for(int j=ub; j<C[i].size(); j++) {
35             temp = min(temp, C[i][j].price);
36         }
37         sum += temp;
38         if(sum > b)    return false;
39     }
40     return true;
41 }
42 // O(n)复杂度的检测
43 bool test2(int x)
44 {
45     int cost, sum=0;
46     for(int i=0; i<tot; i++)
47     {
48         cost = MAXN;
49         for(int j=0; j<C[i].size(); j++)
50         {
51             if(C[i][j].quality >= x)
52                 cost = min(cost, C[i][j].price);
53         }
54         if(cost==MAXN)    return 0;
55         sum += cost;
56     }
57     return sum <= b;
58 }
59 
60 int main ()
61 {
62     string type, name;
63     scanf("%d", &T);
64     while(T--) {
65         scanf("%d%d", &n, &b);
66         int cur = 0, lb, ub;
67         tot = 0;
68         map<string, int> mp;
69         for(int i=0; i<n; i++) {
70             P p;
71             cin >> type >> name >> p.price >> p.quality;
72             if(i == 0 || p.quality > ub)    ub = p.quality; 
73             if(i == 0 || p.quality < lb)    lb = p.quality;
74             
75             if(mp.find(type) == mp.end()) {
76                 cur = tot;
77                 mp[type] = tot++;
78             } else {
79                 cur = mp[type];
80             }
81             C[cur].push_back(p);
82         }
83         for(int i=0; i<tot; i++) {
84             sort(C[i].begin(), C[i].end());
85         }
86         ub++;
87         while(ub - lb > 1) { // [ , )
88             int m = (lb + ub) >> 1;
89 
90             if(test(m))    lb = m;
91             else    ub = m; 
92         }
93         printf("%d\n", lb);
94         for(int i=0; i<tot; i++)
95             C[i].clear();
96     }
97     return 0;
98 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值