简单贪心,注意m和n可能为0的,不小心WA了一次
#include<cstdio>
#include<algorithm>
using namespace std;
typedef struct{
double j;
double f;
double jpf;
}room;
bool cmp(room a,room b){
return a.jpf>b.jpf;
}
int main(){
int n,i;
double m,jbeans;
room r[1000+1];
while(scanf("%lf %d",&m,&n)){
if(m==-1.0&&n==-1) break;
for(i=1;i<=n;i++) {
scanf("%lf %lf",&r[i].j,&r[i].f);
r[i].jpf=r[i].j/r[i].f;
}
sort(r+1,r+n+1,cmp);
jbeans=0;
for(i=1;i<=n&&m>0;i++)
if(m>=r[i].f) {jbeans+=r[i].j;m-=r[i].f;}
else if(m>0) {jbeans+=r[i].jpf*m;m=0;}
printf("%.3lf\n",jbeans);
}
return 0;
}
情况