文章

42

粉丝

0

获赞

35

访问

999

头像
组队刷题 题解:
P1307
发布于2026年3月10日 16:33
阅读数 36

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

typedef struct{
	float volume;
	float price;//price per mL for each beverage;
	float totalprice;
}beverage;
bool cmp(const beverage &x,const beverage &y)
{
	return x.price<y.price;
}
int main()
{
	float x;
	int n;
	while(cin>>x>>n)
	{
		if(x==-1&&n==-1)
			break;
		float mi,wi;
		vector<beverage> list;
		for(int i=0;i<n;i++)
		{
			cin>>mi>>wi;
			list.push_back({mi,	wi/mi,wi});
		}
		sort(list.begin(),list.end(),cmp);
		float maxdrink=0;//maximun volume
		for(int i=0;i<list.size();i++)
		{
			if(x>=list[i].totalprice) // 1. buy out this beverage.
			{
				x-=list[i].totalprice;
				maxdrink+=list[i].volume;
			}
			else{						// 2. buy some of the beverage and go.
				maxdrink+=x/list[i].price;
				break;
			}
		}
		printf("%.3f\n",max...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发