#include<bits/stdc++.h>
using namespace std;
struct MOON
{
double que;
double price;
double aver;
}skr[1010];
bool cmp(const MOON& a,const MOON& b)
{
if(a.aver != b.aver)
return a.aver > b.aver;
else if(a.aver == b.aver && a.que != b.que)
return a.que > b.que;
else if(a.aver == b.aver && a.que == b.que && a.price != b.price)
return a.price < b.price;
}
int main()
{
int N, D;
cin >> N >> D;
for(int i = 0; i < N; i ++)
cin >> skr[i].que;
for(int i = 0; i < N; i ++)
cin >> skr[i].price;
for(int i = 0; i < N; i ++)
skr[i].aver = skr[i].price / skr[i].que;
sort(skr,skr + N, cmp);//排序
double sumprice = 0;
double sumque = 0;
for(int i = 0; i < N; i ++)
{
if(sumque + skr[i].que <= D)
{
sumprice += skr[i].price;
sumque += skr[i].que;
}
else
{
double x = (double)D - sumque;
sumprice += x * skr[i].aver;
break;
}
}
printf("%.2lf",sumprice);
}
使用结构体排序解决了普通贪心(应该是)问题