http://acm.hdu.edu.cn/showproblem.php?pid=2187
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int p,w;
}Rice;
Rice r[1005];
int Compare(const void* a,const void* b)
{
Rice*x=(Rice*)a;
Rice*y=(Rice*)b;
return x->p-y->p;
}
int main()
{
int t,n,m,i,sum_Money;
double sum_Weight;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
scanf("%d %d",&r[i].p,&r[i].w);
qsort(r,m,sizeof(Rice),Compare);
sum_Money=0;
sum_Weight=0.0;
for(i=0;i<n;i++)
{
sum_Money+=r[i].p*r[i].w;
if(sum_Money>n)
{
sum_Money-=r[i].p*r[i].w;
while(sum_Money<=n)
{
sum_Money+=r[i].p;
sum_Weight+=1.0;
}
sum_Money-=r[i].p;
sum_Weight-=1.0;
sum_Weight+=(n-sum_Money)*(1.0/r[i].p);
break;
}
else
{
sum_Weight+=r[i].w*(1.0);
}
}
printf("%.2f\n",sum_Weight);
}
return 0;
}