Assemble UVA - 12124(典型二分答案)

Assemble UVA - 12124(典型二分答案)Assemble UVA 12124 题意 给你 b 元去选购配件 有 n 个配件 每个配件都有种类 名字 品质因子 价格 要求 每种配件购买一个 总价格不超过 b 且最差配件的品质因子 的应尽可能大 思路 典型二分答案 反思 二分 时假如是

大家好,我是讯享网,很高兴认识大家。

Assemble UVA - 12124

题意:

给你b元去选购配件。有n个配件,每个配件都有种类、名字、品质因子、价格。
要求
每种配件购买一个,总价格不超过b,且最差配件的品质因子的应尽可能大。

思路:

典型二分答案。


讯享网

反思:

二分时假如是 w h i l e ( l < = r ) while(l<=r) while(l<=r)
那么中间要 l = m i d + 1 l=mid+1 l=mid+1 和 r = m i d − 1 r=mid-1 r=mid−1
二分时假如是 w h i l e ( l < r ) while(l<r) while(l<r)
那么中间要 l = m i d l=mid l=mid 和 r = m i d − 1 r=mid-1 r=mid−1

AC

#include <iostream> #include <bits/stdc++.h> #define For(i,x,y) for(int i=(x); i<=(y); i++) #define fori(i,x,y) for(int i=(x); i<(y); i++) #define rep(i,y,x) for(int i=(y); i>=(x); i--) #define mst(x,a) memset(x,a,sizeof(x)) #define pb push_back #define sz(a) (int)a.size() #define mp make_pair #define fi first #define se second using namespace std; typedef long long ll; typedef pair<int,int>pa; typedef pair<ll,ll>pai; int n, b; const int maxn = 1e3+10; struct compent{ 
    int pr, qa; }; vector<compent> pp[maxn]; vector<string> tpp; map<string,int>ma; int idc(char * type){ 
    string s(type); if(ma.count(s))return ma[s]; tpp.pb(s); return ma[s] = sz(tpp)-1; } void init(){ 
    For(i,0,n)pp[i].clear(); ma.clear(); tpp.clear(); } bool check(int x){ 
    int res = 0, tot = sz(tpp); fori(i,0,tot){ 
    int tot1 = sz(pp[i]), mi = 1e9+10; fori(j,0,tot1){ 
    if(pp[i][j].qa>=x)mi = min(mi,pp[i][j].pr); } res += mi; if(res>b)return false; } return true; } int main() { 
    //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt; scanf("%d", &tt); while(tt--){ 
    int pr, qa, idx; char name[30], type[30]; scanf("%d%d", &n, &b); init(); int l, r=0, ans; For(i,1,n){ 
    scanf("%s%s%d%d", type, name, &pr, &qa); // cout<<type<<' '<<name<<' '<<pr<<' '<<qa<<endl; idx = idc(type); pp[idx].pb({ 
   pr,qa}); r = max(r,qa); } l = 0; while(l<=r){ 
    int mid = l + (r-l+1)/2;//int mid = (l+r)>>1; if(check(mid))ans=mid,l=mid+1; else r=mid-1; } printf("%d\n", ans); } return 0; } 

讯享网
小讯
上一篇 2025-04-09 10:02
下一篇 2025-02-17 23:25

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/102714.html