Clarke and food

本文介绍了一个经典的背包问题,其中一个人物Clarke患有多种人格障碍,在变为厨师人格时,面临如何用有限背包容量装下尽可能多食材的挑战。通过使用贪心算法,文章提供了一种有效的解决方案,并附上了C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Clarke is a patient with multiple personality disorder. One day, Clarke turned into a cook, was shopping for food.
Clarke has bought n food. The volume of the ith food is vi. Now Clarke has a pack with volume V. He wants to carry food as much as possible. Tell him the maxmium number he can brought with this pack.
Input
The first line contains an integer T(1≤T≤10), the number of the test cases.
For each test case:
The first line contains two integers n,V(1≤n≤105,1≤V≤109).
The second line contains n integers, the ith integer denotes vi(1≤vi≤109).
Output
For each test case, print a line with an integer which denotes the answer.
题意:有一个包 体积是v 后面有n种菜 每种菜有ni个单位体积 问最多能装下多少种菜
题目很简单 思路就不说了 贪心就完事 直接附代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxn=1e5+5;
int main()
{
   int n;
   scanf("%d",&n);
   while(n--)
   {
   	int a[maxn];
   	    int t,v;
   	    scanf("%d%d",&t,&v);
		  for(int i=0;i<t;i++)
		  scanf("%d",&a[i]);
		  sort(a,a+t);
		  int s=0,sum=0;
		  while(s<=v)
		  {		  	
		  	s+=a[sum];
		  	sum++;
				  }

				  printf("%d\n",sum-1);	  
	} 
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值