链接:
@[toc]

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows
eating the grass, as usual. When he returned, he found to his horror
that the cluster of cows was in his garden eating his beautiful
flowers. Wanting to minimize the subsequent damage, FJ decided to take
immediate action and transport each cow back to its own barn. Each cow
i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from
its own barn. Furthermore, while waiting for transport, she destroys
Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ
can only transport one cow at a time back to her barn. Moving cow i to
its barn requires 2 × Ti minutes (Ti to get there and Ti to return).
FJ starts at the flower patch, transports the cow to its barn, and
then walks back to the flowers, taking no extra time to get to the
next cow that needs transport. Write a program to determine the order
in which FJ should pick up the cows so that the total number of
flowers destroyed is minimized.

输入描述:

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

输出描述:

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

flowers
示例1
输入

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

输出

86

题意:

n头牛,将第i头牛运回谷仓需要时间2*ti,在等待运输过程中每分钟吃di朵花,问怎么运输牛才能让花的损失最小?

题解:

贪心问题
有AB两头牛相邻,
A牛:Ta,Da
B牛:Tb, Db
在运A牛时,B牛在等待,等待了Ta2的时长,每分钟吃Db多花,一共吃了Ta2Db多花
同理,在运B牛时,A牛在等待,等待了Tb
2的时长,每分钟吃Da多花,一共吃了Tb2Da多花
当运输A牛比运输B牛更划算时,
Ta2Db<Tb2Da
TaDb<TbDa
然后将牛按照这个排序,这个顺序就是最佳顺序,直接计算花的损失即可

代码:

tot记录一分钟所有等待牛破坏花的数量

#include<bits/stdc++.h>
const int maxn=1e5+3;
using namespace std;
struct node{
    int t,d;
}a[maxn];
bool cmp(node x,node y){
    return x.t*y.d<x.d*y.t;
}
int main()
{
    int n;
    cin>>n;
    long long sum=0;
    long long tot=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].t>>a[i].d;
        tot+=a[i].d;
    }
    sort(a+1,a+n+1,cmp);

    for(int i=1;i<=n;i++)
    {
//        cout<<"tot="<<tot<<endl;
        sum+=(tot-a[i].d)*a[i].t*2;
        tot-=a[i].d;
    }
    cout<<sum;
    return 0;
}