Sicily 1890 BallsAgain

本文通过C++实现了一种瓶子填充算法,该算法根据瓶子容量及数量对球进行分配,旨在最大化利用瓶子的空间。首先对瓶子和球按降序排序,然后逐个尝试将球放入合适的瓶子中,直至所有球都被放置或所有瓶子被填满。

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

#include <iostream>
#include <algorithm>

using namespace std;

struct bottle{
	int num;
	int cap;
};
bottle arr[1000];

bool reduce1(bottle a,bottle b){
	if(a.cap!=b.cap) return a.cap > b.cap;
	return a.num > b.num;
}
bool reduce2(int a,int b){
	return a > b;
}

int ball[1000];

int main()
{
	int n_ball,n_bot;
	while(cin >> n_ball >> n_bot){
		
		for(int i=0; i < n_ball; ++i)
			cin >> ball[i];
		for(int i=0; i < n_bot; ++i)
			cin >> arr[i].num >> arr[i].cap;
		
		sort(arr,arr+n_bot,reduce1);
		sort(ball,ball+n_ball,reduce2);
		
		long long k=0,count_n=0,count_cap=0;
		
		for(int i=0; i < n_ball; ++i){
			while(arr[k].num==0) k ++;
			if(k>=n_bot) break;
			
			if(ball[i]<=arr[k].cap){
				count_cap += ball[i];
				count_n ++;
				arr[k].num --;
			}
			
			
		}
		cout << count_n << " " << count_cap << endl;
	}
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值