uva12124
来源:互联网 发布:软件系统分析师 编辑:程序博客网 时间:2024/05/29 12:03
题目大意:
组装一台电脑,每个配件都需要买一个,总价格不可以超过b,且品质最差配件的品质因子要尽量的大。
思路:
最小值最大化的问题,用二分法,二分法分的是最低的品质因子的范围,判断函数写的是所有的品质因子大于k的时候是否可以组装成电脑,如果可以的话就继续往品质因子大的取,如果不行就要往小的取。
代码:
#include <iostream>using namespace std;#include <cstring>#include <stdio.h>#include <vector>#include <map>const int maxn = 1005;int n,b;struct Component { int price; int quality;};map <string,int> id;int cnt; vector <Component> comp[maxn];int find(char * str) { string s(str); if(!id.count(s)) id[s] = cnt++; return id[s];}bool yes(int k) { int sum = 0; for(int i = 0 ; i < cnt; i++) { int cheapest = b + 1; for(int j = 0 ; j < comp[i].size(); j++) { if(comp[i][j].quality >= k) cheapest = min(cheapest,comp[i][j].price); } if(cheapest == b + 1) return false; sum += cheapest; if(sum > b) return false; } return true;}int main() { int T; int maxq; scanf("%d",&T); while(T--) { //id.clear(); cnt = 0; maxq = 0; char type[30],name[30]; int p,q; scanf("%d %d",&n,&b); id.clear(); for(int i = 0 ; i < n; i++) comp[i].clear(); for(int i = 0 ; i < n; i++) { scanf("%s %s %d %d",type,name,&p,&q); maxq = max(maxq,q); comp[find(type)].push_back((Component) {p,q}); } int l = 0,r = maxq,mid; while(l < r) { mid = (l + r + 1)/2; // 一定要(l + r + 1) /2才不会TLE if(yes(mid)) l = mid; else r = mid - 1; } printf("%d\n",r); } return 0;}
0 0
- uva12124
- UVa12124 - Assemble
- uva12124-Assemble
- uva12124 Assemble
- 【UVA12124】Assemble
- uva12124 组装电脑
- 例题1.12 组装电脑 UVa12124
- POJ3497 UVA12124 Assemble(二分 + 贪心)
- Assemble HDU2333 || uva12124 || poj3497 || zoj3090
- O2O 一比一积分兑换项目 源码实现和框架搭建
- MySqlCommand查询条件中包含中文没有结果的问题
- 常用的相似性度量(距离总结)
- java对mysql的操作
- do while 2
- uva12124
- Java Collections.synchronizedMap方法分析
- 空间向量坐标运算
- UITableView的属性
- Usb Audio Device Descriptor(8) Type I Format Type Descriptor
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree - Codeforces(思维)
- mac 命令大全
- c++ 纯虚函数
- Http通信过程(7步)