牛客网__Protecting the Flowers

链接:https://ac.nowcoder.com/acm/contest/984/I
来源:牛客网
 

时间限制: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

假设当前有A(x1,y1),  B(x2,y2)   剩下的是last   那么现在选A或选B对last 的贡献是没影响的,那么

    先取A 消费再取B:  y2*x1*2+last*x1*2+last*x2*2

    先取B 消费再取A:   y1*x2*2+last*x2*2+last*x1*2

那么其实就可以比较  x1*y2  和  y1*x2

所以按这个结构体排序就可以

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 2e6+9;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;

struct rt{
    int x,y;
}a[maxn];

bool cmp(rt a,rt b){
    return 1ll*a.x*b.y<1ll*a.y*b.x;
}

int main(){
    int n;scanf("%d",&n);
    ll sum=0;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
        sum+=a[i].y;
    }
    sort(a+1,a+1+n,cmp);
//    for(int i=1;i<=n;i++){
//
//    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        sum-=a[i].y;
        ans+=sum*a[i].x*2;
    }
    printf("%lld\n",ans);
    return 0;
}








 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章