[USACO 2007 Jan S]Protecting the Flowers

Posted cq.tiancx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO 2007 Jan S]Protecting the Flowers相关的知识,希望对你有一定的参考价值。

题目: [USACO 2007 Jan S]Protecting the Flowers ,哈哈,我们今天来看一道简单的贪心算法题嘛,这是选自USACO上的一道题,好了,我们一起来看看题意吧:

题目描述是复制的,可能有部分显示不对,我就把题目链接放下面!
题目链接: [USACO 2007 Jan S]Protecting the Flowers

题目大概意思:
一共有n个牛,你把第i头牛带回牛舍需要2∗ti 时间,第i头牛每分钟破坏di朵花,第i头牛每分钟破坏di 朵花
用怎样的顺序能使得破坏的花数最少?

题目描述

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

思路:

假设如果有两头牛,规划一下这两头牛的顺序,假设先领第1头牛回去,损坏的花的数量为2*t1d2,假设先领第2头牛回去,损坏的花的数量为2t2*d1,假设要使先领第一头牛回去,这样损坏的总的花最少,则应该满足这个条件: t1*d2<t2*d1
​OK,具体的思路就看代码吧,有注释的!

我们来看看成功AC的代码吧:

#include<bits/stdc++.h>
using namespace std;
int n;
struct node
    int ti,di;
a[100010];
bool cmp(node x,node y)  return (x.ti*y.di)<(y.ti*x.di); //排序规则
long long ans;//这里记住开longlong ,极限数据int 装不了,在这里我还WA了一次
int main()
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)
        int x,y;    cin>>x>>y;
        a[i]=x,y;
    
    sort(a,a+n,cmp);
    long long time=0;
    for(int i=1;i<n;i++)
        time+=2*a[i-1].ti;
        ans+=time*a[i].di;
    
    cout<<ans;
    return 0;


谢谢你的阅读,由于作者水平有限,难免有不足之处,若读者发现问题,还请批评,在留言区留言或者私信告知,我一定会尽快修改的。若各位大佬有什么好的解法,或者有意义的解法都可以在评论区展示额,万分谢谢。
写作不易,望各位老板点点赞,加个关注!

以上是关于[USACO 2007 Jan S]Protecting the Flowers的主要内容,如果未能解决你的问题,请参考以下文章

[bzoj 1636][Usaco2007 Jan]Balanced Lineup

[Usaco2007 Jan]Running贝茜的晨练计划

[Usaco2007 Jan]Balanced Lineup排队

[bzoj 1699] [Usaco2007 Jan]Balanced Lineup排队

[BZOJ] 1634: [Usaco2007 Jan]Protecting the Flowers 护花

[BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花