LA3971 组装电脑
思路:二分,就是在不超过b的预算下,使得品质的最小值最大化。关键还是判断函数吧。
假设答案为x,判断函数,就是每一个种类的配件的品质最基本的品质要大于x,然后找出最小的值。这样的配件品质之和的价格要小于b元。
则表明x是答案之一。但是,不一定是最优答案。最后答案就要看二分的方向了。
#include<iostream> #include<string> #include<map> #include<vector> #include<algorithm> #include<string> using namespace std; const int maxn = 1e3 + 5; 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 = 0; for (int i = 0; i < cnt; ++i){ int pest = b + 1, m = comp[i].size(); for (int j = 0; j < m;++j) //找大于x的最小值 if (comp[i][j].y >= x)pest = min(pest, comp[i][j].x); if (pest == b + 1)return 0; sum += pest; if (sum>b)return 0; } return 1; } int main(){ cin >> t; while (t--){ cin >> n >> b; //初始化 cnt = 0; for (int i = 0; i < n; ++i)comp[i].clear(); ss.clear(); int maxq = 0; for (int i = 0; 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 = 0, R = maxq; while (L < R){ // cout << "L=" << L << " R=" << R << endl; int M = L + (R - L + 1) / 2; if (ok(M))L = M; else R = M - 1; } cout << L << endl; } return 0; }
作者:ALINGMAOMAO
出处:https://www.cnblogs.com/ALINGMAOMAO/p/10764880.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探