POJ-3264-Protecting the Flowers

来源:互联网 发布:淘宝买家虚假交易 编辑:程序博客网 时间:2024/05/20 12:47

题意:有一群牛正在花园里吃花,给你几组测试分别是把牛运回去的时间和一单位时间里对花的伤害,让你帮助找出按什么顺序把牛运回去才能使牛对花的伤害最小。

思路:很简单,就是对花的伤害除以运回去的时间就是对花的平均伤害,根据平均伤害从大到小排序就可以了。

代码:

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int maxn=100005;struct lpf{    int t;    int d;}a[maxn];int cmp(struct lpf p,struct lpf q){    double x,y;    x=p.d*1.0/p.t;    y=q.d*1.0/q.t;    return x>y;}int main(){    int n;    while(~scanf("%d",&n))    {        long long sum=0,hua=0;        for(int i=0;i<n;i++)        {            scanf("%d%d",&a[i].t,&a[i].d);            sum+=a[i].d;        }        sort(a,a+n,cmp);        for(int i=0;i<n;i++)        {            sum-=a[i].d;            hua+=sum*a[i].t;        }        printf("%lld\n",hua*2);    }}


0 0
原创粉丝点击