hdu2187悼念512汶川大地震遇难同胞——老人是真饿了(贪心 简单题)

时间:2022-06-03 23:13:44

传送门

简单题

 #include<bits/stdc++.h>
using namespace std;
struct node
{
double dan,weight;
}a[];
bool cmp(node x,node y)
{
return x.dan<y.dan;
}
void init()
{
for(int i=;i<;i++)
{
a[i].dan=0.0;a[i].weight=0.0;
}
}
int main()
{
int c;
while(~scanf("%d",&c))
{
while(c--)
{
double n;int m;
scanf("%lf %d",&n,&m);
init();
for(int i=;i<m;i++)
{
scanf("%lf %lf",&a[i].dan,&a[i].weight);
}
sort(a,a+m,cmp);double ans=;
for(int i=;i<m;i++)
{
if(n>)
{
if(n>=a[i].dan*a[i].weight)
{
n-=a[i].dan*a[i].weight;
ans=a[i].weight+ans;
}
else
{
ans=n/a[i].dan+ans;
n=;break;
}
}
}
printf("%.2lf\n",ans);
}
}
return ;
}