首页 > 代码库 > HDU 4442 贪心

HDU 4442 贪心

给出N个数,和每个数的a,b值

贪心思想,按a/b排序放置即可;


#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;

struct node
{
    int a,b;
    double c;
}data[100010];
__int64 inf=99999999999999;
__int64 Mod=365*24*60*60;

bool cmp(node a,node b)
{
    return a.c<b.c;
}
int main()
{
    int n,i;
    __int64 ans,temp;
    while (scanf("%d",&n)!=EOF)
    {
        if (n==0) break;
        for (i=0;i<n;i++)
        {
            scanf("%d%d",&data[i].a,&data[i].b);
            if (data[i].b==0) data[i].c=inf;
            else data[i].c=1.0*data[i].a/data[i].b*1.0;
        }
        sort(data,data+n,cmp);

        ans=temp=0;
        for (i=0;i<n;i++)
        {
            temp=ans;
            temp*=data[i].b;
            temp+=data[i].a;
            ans+=temp;
            ans%=Mod;
        }
        printf("%I64d\n",ans);

    }
    return 0;

}


HDU 4442 贪心