//PAT1020
//先按价值高到低排序,然后贪心
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
using namespace std;
struct Species
{
double stock; //存货
double total; //总价
double price; //单价
Species(double s, double t) : stock(s), total(t)
{
if ( stock > 0 ) //存货量大于0
{
price = 1.0 * total / stock;
}
else //否则不符合要求
price = 0;
}
bool operator>(const Species& sp) const
{
return price > sp.price;
}
};
int main()
{
vector<Species> v;
vector<double> v1;
vector<double> v2;
int n;
double d;
cin>>n>>d;
for (int i=0; i<n; i++)
{
double t;
cin>>t;
v1.push_back(t);
}
for (int i=0; i<n; i++)
{
double t;
cin>>t;
v2.push_back(t);
}
for (int i=0; i<n; i++)
{
v.push_back( Species(v1[i], v2[i]) );
}
sort(v.begin(), v.end(), greater<Species>()); //按单价进行排序(高到低)
double max_benifit = 0; //保存最大收益
for (int i=0; i<v.size() && d > 0; i++)
{
if ( d >= v[i].stock ) //需求量依然超过第i-1中存货量,直接加入
{
d -= v[i].stock;
max_benifit += v[i].total;
}
else //否则截取能加入的
{
max_benifit += v[i].price * d;
d = 0;
}
}
printf("%.2f\n", max_benifit);
return 0;
}
1020 月饼 (25)
最新推荐文章于 2020-09-20 19:08:26 发布