LA3971 组装电脑
2024-10-15 08:36:24
思路:二分,就是在不超过b的预算下,使得品质的最小值最大化。关键还是判断函数吧。
假设答案为x,判断函数,就是每一个种类的配件的品质最基本的品质要大于x,然后找出最小的值。这样的配件品质之和的价格要小于b元。
则表明x是答案之一。但是,不一定是最优答案。最后答案就要看二分的方向了。
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 1e3 + ;
int cnt;
map<string, int>ss;
int id(string x){
if (!ss.count(x))ss[x] = cnt++;
return ss[x];
}
struct node{ int x, y; };
vector<node>comp[maxn];
int t, n, b; //品质不小于x的组件能否组装为不超过b的电脑
bool ok(int x){
int sum = ;
for (int i = ; i < cnt; ++i){
int pest = b + , m = comp[i].size();
for (int j = ; j < m;++j) //找大于x的最小值
if (comp[i][j].y >= x)pest = min(pest, comp[i][j].x);
if (pest == b + )return ;
sum += pest;
if (sum>b)return ;
}
return ;
} int main(){
cin >> t;
while (t--){
cin >> n >> b;
//初始化
cnt = ;
for (int i = ; i < n; ++i)comp[i].clear();
ss.clear(); int maxq = ;
for (int i = ; i < n; ++i){
//初始化
string type, name; int x, y;
cin >> type >> name >> x >> y; maxq = max(maxq, y);
comp[id(type)].push_back(node{ x, y });
}
int L = , R = maxq;
while (L < R){
// cout << "L=" << L << " R=" << R << endl;
int M = L + (R - L + ) / ;
if (ok(M))L = M; else R = M - ;
}
cout << L << endl;
}
return ;
}
最新文章
- C++整数转字符串的一种方法
- java spring 配置文件的读取
- base64与byte[]之间转换
- zabbix 3.0.4 Nginx 性能监控
- .net学习笔记---webconfig的读与写
- iOS 开发之 Xcode6 创建真机调试证书
- [Angular2 Form] Use RxJS Streams with Angular 2 Forms
- 4.2 CUDA Reduction 一步一步优化
- 配置android source 在ubuntu中编译环境
- Android全部权限详解(manifest.xml)
- LeetCode OJ 292.Nim Gam148. Sort List
- Android中微信抢红包助手的实现
- JavaWeb的国际化(17/4/8)
- (转)Spark性能优化:资源调优篇
- C++之标准输入输出
- PAT1055:The World&#39;s Richest
- zabbix使用客户端和不使用客户端监控指定端口
- canvas绘制圆图输出图片格式
- Python全栈开发之路 【第四篇】:Python基础之函数
- 控件_TimePicker