P2878 [USACO07JAN]保护花朵Protecting the Flowers - 贪心

传送门

思路:

对于“找出一种最优排列顺序,使答案最优”的贪心题目,我们可以用“邻项交换”的方法去找出并证明贪心策略。

例如本题,我们假设有两头奶牛,其到达牛圈时间分别为Ti,Ti+1,每分钟吃掉的花朵数分别为Di,Di+1

有两种情况:

① 排列顺序为i  i+1 则两头牛吃掉的花朵数为 res1=2TiDi+1

② 排列顺序为i+1  i 则两头牛吃掉的花朵数为 res2=2Ti+1Di

假设前一种方案是最优解,则res1<res2,即TiDi+1<Ti+1Di

这样我们就找到了一种贪心的排序方法。

其实可以对这个式子移项:Di/Ti>Di+1/Ti+1

这样按每天奶牛的吃花速度与到达牛圈的时间的比值从大到小排列就可以了。

注意:除法有精度问题,需用double存储变量,最后用%.lf输出去掉小数位。

AC Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100000+10;
typedef long long ll;
struct node{
    double d,t;
}a[N];
bool cmp(const node x,const node y){
    return (x.d/x.t)>(y.d/y.t);
}
double s[N];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].t,&a[i].d);
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i].d;
    double ans=0;
    for(int i=1;i<=n;i++) {
        ans+=2*a[i].t*(s[n]-s[i]);
    }
    printf("%.lf",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Loi-Brilliant/p/9419531.html

时间: 2024-08-29 02:39:26

P2878 [USACO07JAN]保护花朵Protecting the Flowers - 贪心的相关文章

洛谷 P2878 [USACO07JAN]保护花朵Protecting the Flowers 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2878 (本来想放bzoj的链接  然而bzoj这道题挂了) [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小,约翰赶紧采取行动,把牛们送回牛棚. 牛们从1到N编

洛谷P2878 [USACO07JAN]保护花朵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

USACO 保护花朵 Protecting the Flowers, 2007 Jan

Description 约翰留下了 N 只奶牛呆在家里,自顾自地去干活了,这是非常失策的.他还在的时候,奶牛像 往常一样悠闲地在牧场里吃草.可是当他回来的时候,他看到了一幕惨剧:他的奶牛跑进了他的花园, 正在啃食他精心培育的花朵!约翰要立即采取行动,挨个把它们全部关回牛棚. 约翰牵走第 i 头奶牛需要 T i 分钟,因为要算来回时间,所以他实际需要 2 · T i 分钟.第 i 头奶牛 如果还在花园里逍遥,每分钟会啃食 Di 朵鲜花.但只要约翰抓住了它,开始牵走它的那刻开始,就 没法吃花了.请帮

poj 3262 Protecting the Flowers 贪心

题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草 求把所有奶牛送回牛棚内,所消耗草的最小值 思路:贪心,假设奶牛a和奶牛b所处位置为, 交换前 ....(ta, da) (tb, db).... 交换后 ....(tb, db) (ta, da).... 设此前已消耗的时间为x,那么交换前消耗的草:x*da + (x+ta)*db 交换后消耗的草:x*db + (x+tb)*da 除非交换后的消耗相比交换前的小才交换

poj -3262 Protecting the Flowers (贪心)

http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛需要的时间和牛在花园每分钟吃多少花朵,问你怎么安排让损失最小. 这题单独按time和eat排序都不行,得按它们的比率来排,如果是选择eat/time  则从大到小排,time/eat则从小到大排,但是不会严格证明. 1 #include <iostream> 2 #include <cstd

POJ3262 Protecting the Flowers 【贪心】

Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4418   Accepted: 1785 Description 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 ho

[bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵花.求一个弄走牛的顺序,使得这些牛破坏最少的花. 注释:$1\le n \le 10^5$. 想法:贪心. 两头牛i和j. 只考虑这两头牛的位置. 如果i在j前面,拉走i的时候j会造成$2t_i*atk_j$朵花.反之同理. 比较两者谁大就放在前面. 在cmp中这样写就行了. 最后,附上丑陋的代码.

Protecting the Flowers(POJ3262)(贪心)

Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4540   Accepted: 1819 Description 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 ho

POJ 3262 Protecting the Flowers(贪心)

Language: Default Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4942   Accepted: 1973 Description Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned,