1020 月饼 (25 分)
简单贪心。贪心算法入门题
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
struct node{
double a,b;
double c;
}ch[10001];
bool cmp(node x,node y){return x.c>y.c;}
int main()
{
int n;
double k;
cin>>n>>k;
for (int i=0;i<n;i++){
cin>>ch[i].a;
}
for (int i=0;i<n;i++){
cin>>ch[i].b;
ch[i].c=ch[i].b/ch[i].a;
}
sort(ch,ch+n,cmp);
double h=0;
for (int i=0;i<n;i++){
if (k>ch[i].a){
h+=ch[i].b;
k-=ch[i].a;
}
else {
h+=ch[i].c*k;
break;
}
}
cout<<fixed<<setprecision(2)<<h;
return 0;
}