赞
踩
#include<cstdio> #include<algorithm> using namespace std; int types,weight;//月饼的种类数 struct Mooncake{ double totalPrice; double price; double weight; double sell;//卖出了多少 }; bool cmp(Mooncake a,Mooncake b){ return a.price>b.price; } int main(){ scanf("%d%d",&types,&weight); Mooncake cakes[types+10]; for(int i=0;i<types;i++){ scanf("%lf",&cakes[i].weight); } for(int i=0;i<types;i++){ scanf("%lf",&cakes[i].totalPrice); } //算出单价 for(int i=0;i<types;i++){ cakes[i].price=cakes[i].totalPrice/cakes[i].weight; } //按照单价从高到低排序 sort(cakes,cakes+types,cmp); //开始贪心 int left= weight;//还需要多少月饼 for(int i=0;i<types;i++){ if(cakes[i].weight<left){//当前单价最高的月饼不足以填补空缺 cakes[i].sell=cakes[i].weight; left-=cakes[i].sell; }else{ cakes[i].sell=left; break; } } int j=0; double ans=0.0; while(cakes[j].sell!=0){ ans+=cakes[j].price*cakes[j].sell; j++; } printf("%.2f\n",ans); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。