2021-04-09

hoj 2068 Assemble

先说好,我的代码oj上过不了,想要抄代码的可以看下一个人的了;

Recently your team noticed that the computer you use to practice for programming contests is not good enough anymore. Therefore, you decide to buy a new computer.

To make the ideal computer for your needs, you decide to buy separate components and assemble the computer yourself. You need to buy exactly one of each type of component.

The problem is which components to buy. As you all know, the quality of a computer is equal to the quality of its weakest component. Therefore, you want to maximize the quality of the component with the lowest quality, while not exceeding your budget.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

    One line with two integers: 1 ≤ n ≤ 1 000, the number of available components and 1 ≤ b ≤ 1,000,000,000, your budget.
    n lines in the following format: "type name price quality", where type is a stringwith the type of the component, name is a string with the unique name of the component, price is an integer (0 ≤ price ≤ 1 000 000) which represents the price of the component and quality is an integer (0 ≤ quality ≤ 1,000,000,000) which represents the quality of the component (higher is better). The strings contain only letters, digits and underscores and have a maximal length of 20 characters.

It will always possible to construct a computer with your budget.

Output

Per testcase: One line with one integer: the maximal possible quality.

Sample Input

1
18 800
processor 3500_MHz 66 5
processor 4200_MHz 103 7
processor 5000_MHz 156 9
processor 6000_MHz 219 12
memory 1_GB 35 3
memory 2_GB 88 6
memory 4_GB 170 12
mainbord all_onboard 52 10
harddisk 250_GB 54 10
harddisk 500_FB 99 12
casing midi 36 10
monitor 17_inch 157 5
monitor 19_inch 175 7
monitor 20_inch 210 9
monitor 22_inch 293 12
mouse cordless_optical 18 12
mouse microsoft 30 9
keyboard office 4 10

Sample Output

9

先说一下总体思想,先定义一个结构体数组a存储数据,

struct zujian{
	char name[20];
	char wuyong[20];
	long long int price;
	long long int value;
};
struct zujian a[1000],b[100]

结构体前2个变量代表组件的名字后2个分别代表组件的价格和性能

再定义另一个结构体b将计算机每一个组件的最小值全存进去

以案例来说,就是这几个数据,也很好实现,

processor 3500_MHz 66 5
memory 1_GB 35 3
mainbord all_onboard 52 10
harddisk 250_GB 54 10
casing midi 36 10
monitor 17_inch 157 5
mouse cordless_optical 18 12
keyboard office 4 10

 

有个头文件<string.h>自带函数strcmp(字符串a,字符串b)

如果字符串a和b完全一样,则会返回0,否则会返回其它数

具体实现:

b[0]=a[0];
for(i=1;i<n;i++)
    if(strcmp(b[j].name,a[i].name)!=0) {b[++j]=a[i];cout++;}
if(strcmp(a[n-2].name,a[n-1].name)!=0) {b[j]=a[n-1];cout++;}

存储完之后你会发现数组b包含了一个电脑的所有组件

一个电脑的性能如何要看它的性能短板

所以要将电脑b所有组件按照性能给它排个序

for(i=0;i<cout-1;i++)
for(j=i;j<cout;j++)
if(b[i].value>b[j].value) 
{
strcpy(c.name,b[j].name);c.price=b[j].price;c.value=b[j].value;			strcpy(b[j].name,b[i].name);b[j].price=b[i].price;b[j].value=b[i].value;
strcpy(b[i].name,c.name);b[i].price=c.price;b[i].value=c.value;
}//
strcpy(a,b)将b换给a

排完序之后电脑b的z数据是这样的

memory 1_GB 35 3
processor 3500_MHz 66 5
monitor 17_inch 157 5
mainbord all_onboard 52 10
harddisk 250_GB 54 10
casing midi 36 10
keyboard office 4 10
mouse cordless_optical 18 12

你可以看到最后1一列的数据:性能是从小到大排序的

它最大能发挥出的性能就是3

但是memory这个组件还有好几个值

在钱充足的情况下我们可以更新它

for(i=0;i<n;i++)
if(strcmp(a[i].name,b[0].name)==0&&a[i].value>b[0].value) 
首先从总数据a中找到相同的组件,再找性能好的
找到了就更新电脑b
{momy1+=a[i].price-b[0].price;
b[0].value=a[i].value;
b[0].price=a[i].price;}

 现在它最弱的性能更新了,变得不一定最弱了,

要知道下一个最弱的性能,就得继续排一下序;

此外,电脑b现在的组件都是要钱的,在钱够的情况下

才能一直更新,所以momy1就有了,它代表当前电脑的价格

这价格要小于预算800.

当更新到要超出预算时就不能更新了

这时候电脑b最上方的那个组件就是最弱性能组件

电脑能发挥的最大性能取决于它,它就是答案(案例输出)

momy1+=a[i].price-b[0].price;
if(momy1>momy) goto loop;
loop:printf("%lld\n",b[0].value);

我用的是goto loop;跳出循环,执行到它后会自动跳到loop:后,

然后再开始执行下面的程序

完整代码:由于根本过不了oj我就把程序改了下输出电脑b的所有组件了

#include<stdio.h>
#include<string.h>
struct zujian{
	char name[20];
	char wuyong[20];
	long long int price;
	long long int value;
};
int main()
{
	int d;
	scanf("%d",&d);
	while(d--)
	{
		long long int n,momy,i,j=0,cout=0,momy1=0;
		struct zujian a[1000],b[100],c;
		scanf("%lld %lld",&n,&momy);
		for(i=0;i<n;i++)
		scanf("%s %s %d %d",&a[i].name,&a[i].wuyong,&a[i].price,&a[i].value);
		b[0]=a[0];
		for(i=1;i<n;i++)
		if(strcmp(b[j].name,a[i].name)!=0) {b[++j]=a[i];cout++;}
		if(strcmp(a[n-2].name,a[n-1].name)!=0) {b[j]=a[n-1];cout++;}
		for(i=0;i<cout-1;i++)
			for(j=i;j<cout;j++)
				if(b[i].value>b[j].value) 
				{
					strcpy(c.name,b[j].name);c.price=b[j].price;c.value=b[j].value;
					strcpy(b[j].name,b[i].name);b[j].price=b[i].price;b[j].value=b[i].value;
					strcpy(b[i].name,c.name);b[i].price=c.price;b[i].value=c.value;
				}
		for(i=0;i<cout;i++)
		momy1+=b[i].price;
		while(momy1<momy)
		{
			for(i=0;i<n;i++)
			if(strcmp(a[i].name,b[0].name)==0&&a[i].value>b[0].value) 
				{momy1+=a[i].price-b[0].price;
				if(momy1>momy) goto loop;
				b[0].value=a[i].value;
				b[0].price=a[i].price;}
			loop:for(i=0;i<cout-1;i++)
				for(j=i;j<cout;j++)
				if(b[i].value>b[j].value) 
				{
					strcpy(c.name,b[j].name);c.price=b[j].price;c.value=b[j].value;
					strcpy(b[j].name,b[i].name);b[j].price=b[i].price;b[j].value=b[i].value;
					strcpy(b[i].name,c.name);b[i].price=c.price;b[i].value=c.value;
				}
		}
		for(i=0;i<cout;i++)
		printf("%s %lld %lld\n",b[i].name,b[i].value,b[i].price);
	}
}

过不了,真的服,最服的是还看不了哪个案例过不了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值