LA 3971 Assemble 组装电脑

题意:给定电脑的n个配件,每个配件有类型,名字(没用的信息),价格和品质因子。要求每种类型的配件各买一个用于组装电脑,总价格不超过b元。求所有可能的方案中品质因子最差的那个配件的品质因子最大能是多少。





非常的暴力。直接枚举每个配件的品质因子作为品质因子最差的配件,判断该配件的品质因子能否满足要求。能满足则更新最大值。判断方法很简单,对于每种类型的配件选择品质因子不低于当前要判断的数且最便宜的购买。若不超过b元则符合要求否则不符合要求。O(n^2)复杂度。n最大才1000。因此暴力水之。





#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int MAX = 1005;
const int INF = 1000000001;
struct Components //组件结构体
{
    int price; //价格
    int quality; //品质因子
};

int n, b;
char type[30], name[30];
Components temp;

int cnt;
map <string, int> id; //对组件种类编号
vector <Components> com[MAX]; //组件

bool judge(int x)
{
    int i, j, sum = 0;
    for(i = 1; i <= cnt; i++)
    {
        int cheapest = INF;
        for(j = 0; j < com[i].size(); j++)
        {
            if(com[i][j].quality >= x)
                cheapest = min(cheapest, com[i][j].price);
        }
        if(cheapest == INF)
            return false;
        sum += cheapest;
    }
    return sum <= b;
}

void input()
{
    scanf("%d%d", &n, &b);
    id.clear();
    for(int i = 0; i < MAX; i++)
        com[i].clear();
    cnt = 0;
    for(int i = 0; i < n; i++)
    {
        scanf("%s%s%d%d", type, name, &temp.price, &temp.quality);
        if(id[type] == 0)
            id[type] = ++cnt;
        com[id[type]].push_back(temp);
    }
}

void solve()
{
    int ans = 0;
    for(int i = 1; i <= cnt; i++)
    {
        for(int j = 0; j < com[i].size(); j++)
        {
            if(judge(com[i][j].quality))
                ans = max(ans, com[i][j].quality);
        }
    }
    printf("%d\n", ans);
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        input();
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值