思路:按照性价比排序,然后贪心选择贵的先卖。注意有的地方要用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
*/
本文介绍了一种基于贪心策略的算法实现,该算法通过计算物品的性价比并按其排序来决定售卖顺序,以最大化收益。适用于需要解决资源分配或物品售卖等场景的问题。
1202

被折叠的 条评论
为什么被折叠?



