题目描述

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.

有n头奶牛跑到FJ的花园里去吃花儿了,它们分别在距离牛圈T分钟处吃花儿,每分钟会吃掉D朵卡哇伊的花儿,(此处有点拗口,不要在意细节啊!),FJ现在要将它们给弄回牛圈,但是他每次只能弄一头回去,来回用时总共为2*T分钟,在这段时间内,其它的奶牛会继续吃FJ卡哇伊的花儿,速度保持不变,当然正在被赶回牛圈的奶牛就没口福了!现在要求以一种最棒的方法来尽可能的减少花儿的损失数量,求奶牛吃掉花儿的最少朵数!

输入输出格式

输入格式:

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
输出样例#1:

86

说明

FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.

题目大意:n头牛吃草,每头牛有每分钟吃多少草,把这头牛运走需要多少时间,求怎样运牛,使牛吃更少的草。

题解:根据每分钟吃的花和运输时间的比值从大到小运...

如果先运j,i牛产生的贡献是cow[i].d*cow[j].t,如果先运i,j牛产生的贡献是cow[j].d*cow[i].t

假设前者比后者小,则,cow[i].d*cow[j].t<cow[j].d*cow[i].t,那么先运j更优,

更优的条件是,cow[i].d/cow[i].t<cow[j].d/cow[j].t为贪心的公式

代码:

#include<iostream>
#include<cstdio>
#include<cstdio>
#include<algorithm>
using namespace std; int sum,n;
long long ans; struct COW{
int t,d;
}cow[]; bool cmp(COW a,COW b){
return a.d*1.0 /a.t>b.d*1.0 /b.t;
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&cow[i].t,&cow[i].d),sum+=cow[i].d;
sort(cow+,cow+n+,cmp);
// for(int i=1;i<=n;i++)cout<<cow[i].d<<endl;
for(int i=;i<=n;i++){
sum-=cow[i].d;
ans+=sum*cow[i].t*;
}
cout<<ans<<endl;
return ;
}

洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers的更多相关文章

  1. 洛谷——P2878 [USACO07JAN]保护花朵Protecting the Flowers

    P2878 [USACO07JAN]保护花朵Protecting the Flowers 题目描述 Farmer John went to cut some wood and left N (2 ≤ ...

  2. bzoj1634 / P2878 [USACO07JAN]保护花朵Protecting the Flowers

    P2878 [USACO07JAN]保护花朵Protecting the Flowers 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...

  3. P2878 [USACO07JAN]保护花朵Protecting the Flowers

    一个类似国王游戏的贪心 话说要是先做了这个题,国王游戏之余懵逼这么久吗? #include<iostream> #include<cstdio> #include<alg ...

  4. Luogu_2878_[USACO07JAN]保护花朵Protecting the Flowers

    题目描述 Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. ...

  5. USACO 保护花朵 Protecting the Flowers, 2007 Jan

    Description 约翰留下了 N 只奶牛呆在家里,自顾自地去干活了,这是非常失策的.他还在的时候,奶牛像 往常一样悠闲地在牧场里吃草.可是当他回来的时候,他看到了一幕惨剧:他的奶牛跑进了他的花园 ...

  6. BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花

    [题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...

  7. 洛谷P2879 [USACO07JAN]区间统计Tallest Cow

    To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. ...

  8. 洛谷 P3299 [SDOI2013]保护出题人 解题报告

    P3299 [SDOI2013]保护出题人 题目描述 出题人铭铭认为给SDOI2012出题太可怕了,因为总要被骂,于是他又给SDOI2013出题了. 参加SDOI2012的小朋友们释放出大量的僵尸,企 ...

  9. 洛谷 P2879 [USACO07JAN]区间统计Tallest Cow

    传送门 题目大意: n头牛,其中最高身高为h,给出r对关系(x,y) 表示x能看到y,当且仅当y>=x并且x和y中间的牛都比 他们矮的时候,求每头牛的最高身高. 题解:贪心+差分 将每头牛一开始 ...

随机推荐

  1. GS与MS之间通信

    GS与MS之间通信 注意GS与MS是两个线程,现在是每个map一个线程,他们之间是内部协议进行通信的,那既然是两个线程那如何通信呢,看了net进程通信这个就比较简单了 举个例子 m_pMap-> ...

  2. node.js实现国标GB28181流媒体点播(即实时预览)服务解决方案

    背景 28181协议全称为GB/T28181<安全防范视频监控联网系统信息传输.交换.控制技术要求>,是由公安部科技信息化局提出,由全国安全防范报警系统标准化技术委员会(SAC/TC100 ...

  3. jar -cmf file1 file2 file3命令

    jar -cmf file1 file2 file3中的参数c.m.f和file1.file2.file3是一一对应的. 也就是说,file1是输出的.jar文件,file2是往META-INF/MA ...

  4. JAVA NIO 简介 (netty源码死磕1.1)

    [基础篇]netty 源码死磕1.1:  JAVA NIO简介 1. JAVA NIO简介 Java 中 New I/O类库 是由 Java 1.4 引进的异步 IO.由于之前老的I/O类库是阻塞I/ ...

  5. MyEclipse2014新增bug,尝鲜的朋友需注意NotFoundException: org.springframework.web.context.ContextLoaderListener

    事实上标题后面加上这个异常信息并不合适,可是为了方便和我遇到相同问题的童鞋搜到这篇文章.我不得不这样写啦. 这个异常和你的程序没有关系,假设你没有忘记增加spring Jar包的话,这是fucking ...

  6. java基础入门之九九乘法表

    /* 自学java 九九乘法表 Power by Stuart Date: 2015.4.23 */public class Math { public static void main (Strin ...

  7. Linux基础系列:常用命令(7)_正则表达式

    一.环境边量 每个用户登录shell需要执行的四个文件 /etc/profile /home/egon/.bashrc_profile /home/egon/.bashrc /etc/bashrc 非 ...

  8. 牛客练习赛13 A 幸运数字Ⅰ 【暴力】

    题目链接 https://www.nowcoder.com/acm/contest/70/A 思路 暴力每一个子串 用 MAP 标记一下 然后 最后 遍历一遍 MAP 找出 出现次数最多 并且 字典序 ...

  9. BOM之history

    history是JavaScript中BOM上的一个对象,其中存储了浏览器的历史记录 history存储简单过程 浏览器会将一个窗口中访问的网页进行记录,不管我们通过以下哪种方式改变页面,浏览器都会把 ...

  10. MySQL 及 SQL 注入与防范方法

    所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 我们永远不要信任用户的输入,我们必须认定用户输入的数据都是不安全的, ...