您的位置:首页 > 其它

LA 3971 Assemble 组装电脑

2015-07-04 05:08 267 查看
题意:给定电脑的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: