本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2187
本题将单价升序排列,总重量计算较简单,AC代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node
{
int price;
int weight;
}a[1100]; //数组开小提交错了多次!!!
int cmp(node p,node q)
{
if(p.price!=q.price)
return p.price<q.price;
else return p.weight>q.weight;
}
int main()
{
int c;
scanf("%d",&c);
while(c--)
{
int n,m;
double sum;
scanf("%d%d",&n,&m);
int i;
for(i=0;i<m;i++)
{
scanf("%d%d",&a[i].price,&a[i].weight);
}
sort(a,a+m,cmp);
sum=0;
for(i=0;i<m;i++)
{
if(n>=a[i].price*a[i].weight)
{
sum+=a[i].weight;
n-=a[i].price*a[i].weight;
}
else
{
sum+=n*1.0/a[i].price;
break;
}
}
printf("%.2lf\n",sum);
}
return 0;
}