挑战程序设计竞赛2.2习题: Protecting the Flowers POJ - 3262

Protecting the Flowers

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.

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

Hint

FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.
样例坑啊,它不管是时间优先还是毁坏程度优先都能成,但是都不是正确答案,正确方法应该是采用ti/di进行排序,从小到大进行才能得到正确结果,简要证明如下(摘自https://www.xuebuyuan.com/1260897.html):

1 在二个中间选择之中,能根据time/eat小的那个为最优解
证明:
二个羊中 A,B,属性分别为分别为eatA,timeA,eatB,timeB
选A的时候损失timeA*eatB
选B的时候损失timeB*eatA
双方同除以eatA*eatB.
令time/eat为一个羊的比率x
可以证明x小的那个为最优解.

定理,如果有一个选择在n个选择中是最优的,那么(在包含这个选择的)n-1个选择中也是最优的,

逆否命题,如果一个选择在n-1个选择中不是最优的,那么在n个选择中也不是最优的.

推论,如果一个选择在2个选择中不是最优的,那么在n个选择中也不是最优的

所以将羊按照比率x排序,可以找到一个羊A在与其它所有羊比较的过程中都是最优的,

或者说其它n-1的羊在与别的羊做两个之间选一个的选择的时候不是最优的,所有这n-1个羊不是最优解的.
所以羊A是n头羊中最优解
 本人AC代码:
#include <stdio.h>
#include <algorithm>
using namespace std;
struct Node {
    int d;
    int t;
    double s;
    friend bool operator <(Node x, Node y)
    {
        return x.s < y.s;
    }
}cows[100005];
long long total_dis;
long long ans;
int main(void)
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d %d", &cows[i].t, &cows[i].d);
        cows[i].s = (1.0 * cows[i].t) / cows[i].d;
        total_dis += cows[i].d;
    }
    sort(cows, cows + n);
    for(int i = 0; i < n; i++)
    {
        total_dis -= cows[i].d;
        ans += total_dis * cows[i].t * 2;
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/jacobfun/p/12205620.html