#include<cstdio>
#include<algorithm>
using namespace std;
struct Mooncake{
double inventory; //库存
double total_value; //总价
double unit_price; //单价
}cake[1001];
bool cmp(Mooncake a, Mooncake b){
return a.unit_price > b.unit_price; //按照单价从高到低
}
int main(){
int N, D;
scanf("%d %d", &N, &D);
for(int i = 0; i < N; i++){
scanf("%lf", &cake[i].inventory);
}
for(int i = 0; i < N; i++){
scanf("%lf", &cake[i].total_value);
cake[i].unit_price = cake[i].total_value / cake[i].inventory; //计算单价
}
sort(cake, cake + N, cmp); //排序
double total_cost = 0.0;
for(int i = 0; i < N; i++){
if(D > cake[i].inventory){ //需求大于库存
D -= cake[i].inventory; //第i种月饼全部卖出
total_cost += cake[i].total_value; //这些月饼的总价算进花费
}else{ //需求小于库存
total_cost += cake[i].unit_price * D; //只卖出剩余需求量的月饼
break;
}
}
printf("%.2f", total_cost);
return 0;
}
原文地址:https://www.cnblogs.com/zjsaipplp/p/10425236.html
时间: 2024-10-30 16:11:05