思路:按照性价比排序,然后贪心选择贵的先卖。注意有的地方要用double就可以了。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 5;
int N;
double D;
pair<double, double> a[MAXN];
bool cmp(const pair<double, double> x, const pair<double, double> y)
{
return x.first / x.second > y.first / y.second;
}
int main()
{
scanf("%d%lf", &N, &D);
for (int i = 0; i < N; i++) scanf("%lf", &a[i].second);
for (int i = 0; i < N; i++) scanf("%lf", &a[i].first);
sort(a, a + N, cmp);
double ans = 0;
for (int i = 0; i < N; i++)
{
if (a[i].second < D)
{
ans += a[i].first;
D -= a[i].second;
}
else
{
ans += double(a[i].first / a[i].second * D);
break;
}
}
printf("%.2f\n", ans);
return 0;
}
/*
3 20
18 15 10
75 72 45
*/