poj-3262-Protecting the Flowers

 题意:

有n头牛,每头牛每分钟对花的伤害为D,每头牛被运回牛棚需要T分钟,求对花最少的伤害。

解题思路:

只需按照每头牛对花的伤害排序即可,伤害最大的先运回去。

程序:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
#define N 110000
using namespace std;
int d[N];
struct In
{
    long long a;
    long long b;
}cow[N];
bool cmp(In x , In y)
{
    return x.a*y.b>=x.b*y.a;
}
int main()
{
    int n,m,s,t,i,j;
    long long ans;
    while(scanf("%d",&t)!=EOF)
    { 
        memset(d,0,sizeof(d)); 
        for(i=0;i<t;i++)
        {
            cin>>cow[i].a>>cow[i].b;   // 按照伤害,由小到大排序。 
        }
        sort(cow,cow+t,cmp);
       /* for(i=0;i<t;i++)
        {
            cout<<cow[i].a<<" "<<cow[i].b<<endl;; 
        }*/
        d[0]=cow[0].b;
        for(i=1;i<t;i++)
        {
            d[i]=d[i-1]+cow[i].b;
        }
        ans=0;
        for(i=t-1;i>0;i--)
        {
            ans+=d[i-1]*cow[i].a*2;   // 先把伤害大的运回去。 
        }
        cout<<ans<<endl;
        
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值