补题向 | B - Burrito King(贪心)

按照ai/bi降序排序,依次选择直到满足b的总和等于B,若a总和不能超过A,则“-1 -1“

(!!!Tips    bi可能是0;x.a/x.b>y.a/y.b == x.a*y.b>y.a*x.b)

虽然给的样例长这样,但是题目给了误差范围,实际输出会卡精度

#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<fstream>
#include<math.h>
#include<stack>
using namespace std;
typedef long long ll;
const double eps=0.000000001;
int n;
double A,B;
struct F{
	double g;
	int i;
	double a;
	double b;
};
F fo[100005];
bool cmp(F x,F y){
	return x.a*y.b>y.a*x.b;
}
double ans[100005];
int main(){
	freopen("burrito.in","r",stdin);
    freopen("burrito.out","w",stdout);
    scanf("%d%lf%lf",&n,&A,&B);
	for(int i=0;i<n;i++){
		scanf("%lf%lf%lf",&fo[i].g,&fo[i].a,&fo[i].b);
		fo[i].i=i;
	}
	sort(fo,fo+n,cmp);
	double as=0,bs=0;

	
	as=fo[0].g*fo[0].a;
	bs=fo[0].g*fo[0].b;
	memset(ans,0,sizeof(ans));
	bool f=0;
	if(bs>B){
		bs=B;
		B/=fo[0].b;
		as=B*fo[0].a;
		ans[fo[0].i]=B;
		
	}
	else{
		ans[fo[0].i]=fo[0].g;
		for(int i=1;i<n;i++){
			if(f==0){
				double k=fo[i].b*fo[i].g;
				if(k+bs<=B){
					bs+=k;
					as+=fo[i].g*fo[i].a;
					ans[fo[i].i]=fo[i].g;
				}
				else{
					k=(B-bs)/fo[i].b;	
					bs=B;
					as+=fo[i].a*k;
					ans[fo[i].i]=k;
					f=1;
				}
			}
			else{
				break;
			}
		}
		
	}

	if(as<A)printf("-1 -1\n");
	else{
		printf("%.8lf %.8lf\n",as,bs);
		for(int i=0;i<n;i++){
			printf("%.8lf",ans[i]);
			if(i==n-1)printf("\n");
			else printf(" ");
		}
	}


	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bekote

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值