[USACO 2007 Jan S]Protecting the Flowers

简介: [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;
}


相关文章
|
9月前
USACO1.3 修理牛棚
USACO1.3 修理牛棚
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
洛谷P2871-[USACO07DEC]Charm Bracelet S(01背包模板题)
|
移动开发
洛谷P2639-[USACO09OCT]Bessie‘s Weight Problem G(01背包)
洛谷P2639-[USACO09OCT]Bessie‘s Weight Problem G(01背包)
洛谷P2639-[USACO09OCT]Bessie‘s Weight Problem G(01背包)
|
Java 定位技术
杭电bfs 水题1241 Oil Deposits - 油田 详解 + 分析
<div class="panel_content"> <span style="font-size:18px"><span style="background-color:rgb(255,255,153)"><br></span></span> <div class="panel_bottom"> <h1 style="color:#1A5CC8"><span style="back
3053 0
|
人工智能
USACO/friday
Friday the Thirteenth 黑色星期五 描述 13号又是一个星期五。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的 一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400.
882 0
|
SDN
USACO/gift1
描述  对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少。 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人。 然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较多的钱。
1036 0