牛客網__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;
}








 

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