原题: http://acm.hdu.edu.cn/showproblem.php?pid=2187
//用粮食的单价从小到大排序,依次遍历
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct A
{
double w;
double m;
}a[1001];
int cmp(A a,A b)
{
return a.m<b.m;
}
int main()
{
double money;
int m;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lf %d",&money,&m);
for(int i=0;i<m;i++)
{
scanf("%lf %lf",&a[i].m,&a[i].w);
}
sort(a,a+m,cmp);
double wei=0;
for(int i=0;i<m;i++)
{
if(money/a[i].m<=a[i].w)
{
wei=wei+money/a[i].m;
break;
}else{
wei=wei+a[i].w;
money-=a[i].m*a[i].w;
}
}
printf("%.2lf\n",wei);
}
return 0;
}