把比值从大到小排列即可
贪心基础题
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-6
struct candy
{
int v;
int w;
bool operator<(candy c)
{
return double(v)/w - double(c.v)/c.w > eps;
}
};
int main()
{
int n, maxw;
cin >> n >> maxw;
candy a[101];
for(int i = 0;i<n;i++)
{
cin >> a[i].v >> a[i].w;
}
sort(a, a+n);
int curw =0;
double curv = 0;
for(int i = 0; i< n;i++)
{
curv += a[i].v;
curw += a[i].w;
if(curw> maxw)
{
curv-=a[i].v;
curw -= a[i].w;
curv += (double)(maxw-curw)/a[i].w*a[i].v;
break;
}
}
cout << curv << endl;
return 0;
}