USACO2007 Protecting the Flowers /// 比值 前缀和 oj21161

题目大意:

 有N (2 ≤ N ≤ 100,000) 头牛偷吃花

将牛赶回牛棚需Ti minutes (1 ≤ Ti ≤ 2,000,000)

每头牛每分钟能吃Di (1 ≤ Di ≤ 100) 朵花

则赶牛的来回需2*Ti minutes (Ti to get there and Ti to return)

Input

* Line 1: A single integer N

* Lines 2..N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow's characteristics

Output

* Line 1: A single integer that is the minimum number of destroyed flowers

Sample Input

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample Output

86

 

一开始用DFS 结果TLE

#include <bits/stdc++.h>
using namespace std;
long long n,ans,sum;
struct cow
{
    double t,f,s;
}a[100005];
bool cmp(struct cow a,struct cow b)
{
    return a.s>b.s;
}
int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf",&a[i].t,&a[i].f);
        a[i].s=a[i].f/a[i].t;    ///s为每分钟吃花的数量与赶牛时间的比值     
        选取耗时少且吃花多的牛能最大限度地减少损失 则应先赶s更大的牛
        sum+=a[i].f;    ///利用前缀和 省略每次重新循环计量的浪费
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        sum-=a[i].f;
        ans+=a[i].t*sum*2;
    }
    printf("%lld\n",ans);

    return 0;
}        
View Code

 

转载于:https://www.cnblogs.com/zquzjx/p/8359509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值