1. 程式人生 > >hdu 4882 ZCC Loves Codefires(貪心)

hdu 4882 ZCC Loves Codefires(貪心)

string.h struct hdu cmp using return 貪心 ace algo

# include<stdio.h>
# include <algorithm>
# include <string.h>
using namespace std;
struct node
{
    int v;
    int t;
};
struct node a[100010];
bool cmp(node a,node b)
{
    return  a.v *a.t+(a.v+b.v)*b.t<b.v*b.t+(a.v+b.v)*a.t;
}
int main()
{
    int n;
    __int64 sum;
    int i;
    while(~scanf("%d",&n))
    {
        
        for(i=0;i<n;i++)
            scanf("%d",&a[i].v);
        for(i=0;i<n;i++)
            scanf("%d",&a[i].t);
        sort(a,a+n,cmp);
        __int64 sum1=0;
        sum=0;
        for(i=0;i<n;i++)
        {
            sum = (a[i].v+sum);
            sum1+=sum*a[i].t;
        }
        printf("%I64d\n",sum1);
    }
    return 0;
}

hdu 4882 ZCC Loves Codefires(貪心)