天天看点

HDU 2187 悼念512汶川大地震遇难同胞——老人是真饿了

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2187

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cstring>
using namespace std;
int t;
int n,m;
double ans;
struct Node
{
    int p,h;
} a[1005];
bool cmp(struct Node x,struct Node y)
{
    return x.p<y.p;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(a,0,sizeof(a));
        for(int i=0; i<m; i++)
            scanf("%d%d",&a[i].p,&a[i].h);
        sort(a,a+m,cmp);
        ans=0.0;
        for(int i=0; i<m; i++)
        {
            n-=1.0*a[i].p*a[i].h;
            if(n<0)
            {
                n+=1.0*a[i].p*a[i].h;
                ans+=1.0*n/a[i].p;
                n=0;
            }
            else
            {
                ans+=1.0*a[i].h;
            }
            if(n==0.0)
                break;
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}