1634: [Usaco2007 Jan]Protecting the Flowers 护花

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 885  Solved: 575
[Submit][Status][Discuss]

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 horror that the cows were in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport the cows back to their barn. Each cow i is at a location that is Ti minutes (1 <= Ti <= 2,000,000) away from the 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 the barn. Moving cow i to the barn requires 2*Ti minutes (Ti to get there and Ti to return). 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只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小,约翰赶紧采取行动,把牛们送回牛棚. 牛们从1到N编号.第i只牛所在的位置距离牛棚Ti(1≤Ti《2000000)分钟的路程,而在约翰开始送她回牛棚之前,她每分钟会啃食Di(1≤Di≤100)朵鲜花.无论多么努力,约翰一次只能送一只牛回棚.而运送第第i只牛事实上需要2Ti分钟,因为来回都需要时间.    写一个程序来决定约翰运送奶牛的顺序,使最终被吞食的花朵数量最小.

Input

* 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

第1行输入N,之后N行每行输入两个整数Ti和Di.

Output

* Line 1: A single integer that is the minimum number of destroyed flowers

一个整数,表示最小数量的花朵被吞食.

Sample Input

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample Output

86

HINT

约翰用6,2,3,4,1,5的顺序来运送他的奶牛.

Source

Silver

考虑相邻的两只牛交换的条件是d[b]*t[a]<d[a]*t[b]。

按条件排序即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
LL n;
struct data{
LL t,d;
bool operator <(const data tmp)const {
return tmp.d*t<d*tmp.t;
}
}a[];
int main(){
scanf("%lld",&n);
for(int i=;i<=n;i++) scanf("%lld%lld",&a[i].t,&a[i].d);
sort(a+,a+n+);
LL sum=,tim=;
for(int i=;i<=n;i++){
sum+=a[i].d*tim;
tim+=a[i].t*;
}
printf("%lld\n",sum);
return ;
}

[BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心的更多相关文章

  1. BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 448  So ...

  2. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )

    考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...

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

    Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...

  4. 【bzoj1634】[Usaco2007 Jan]Protecting the Flowers 护花 贪心

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

  5. 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  So ...

  6. 【BZOJ】1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1634 贪心.. 我们发现,两个相邻的牛(a和b)哪个先走对其它的牛无影响,但是可以通过 a的破坏花× ...

  7. BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634 题意: 约翰留下他的N只奶牛上山采木.可是,当他回来的时候,他看到了一幕惨剧:牛们正 ...

  8. bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花【贪心】

    因为交换相邻两头牛对其他牛没有影响,所以可以通过交换相邻两头来使答案变小.按照a.t*b.f排降序,模拟着计算答案 #include<iostream> #include<cstdi ...

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

    Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the g ...

随机推荐

  1. Pycharm设置Python的路径

    1. 打开文件->默认设置 2. 找到Python的路径即可,如果没有的话,这里也可以安装一个,只是时间比较久. 3. 选择本地 4. 选择文件

  2. Spark Streaming实例

    Spark Streaming实例分析 2015-02-02 21:00 4343人阅读 评论(0) 收藏 举报  分类: spark(11)  转载地址:http://www.aboutyun.co ...

  3. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目3

    2014-03-20 03:01 题目:给定一个已按升序排序的数组,找出是否有A[i] = i的情况出现. 解法1:如果元素不重复,是可以严格二分查找的. 代码: // 9.3 Given a uni ...

  4. python练习1 ——菱形打印

    具体请看链接: 链接 2018-09-29 12:51:45

  5. FOJ Problem 1016 无归之室

     Problem 1016 无归之室 Accept: 926    Submit: 7502Time Limit: 1000 mSec    Memory Limit : 32768 KB  Prob ...

  6. Apache实现一个ip(如:127.0.0.1)和多个域名(虚拟主机)绑定

    今天在学习PHP时,有这样的一个需求:一个ip(如:127.0.0.1)和多个域名(虚拟主机)绑定,以下是我的解决方案:对Apache进行相关的配置 解决方案一:通过端口来区分不同的虚拟主机 ①按照绑 ...

  7. rsync同步数据

    1. rsync 命令格式rsync [OPTION]... SRC DESTrsync [OPTION]... SRC [USER@]HOST:DESTrsync [OPTION]... [USER ...

  8. Java8并行流使用注意事项

    对于从事Java开发的童鞋来说,相信对于Java8的并行流并不陌生,没错,我们常常用它来执行并行任务,但是由于并行流(parallel stream)采用的是享线程池,可能会对我们的性能造成严重影响, ...

  9. 【距离GDOI:128天】【POJ2778】DNA Sequence(AC自动机+矩阵加速)

    已经128天了?怎么觉得上次倒计时150天的日子还很近啊 ....好吧为了把AC自动机搞透我也是蛮拼的..把1030和这道题对比了无数遍...最终结论是...无视时间复杂度,1030可以用这种写法解. ...

  10. RocketMQ 源码分析 RouteInfoManager(四)

    在上一章分析了NamesrvController的构造函数时,会生成一个RouteInfoManager对象,该对象存放着整个消息集群的相关消息,所以这里单独拿出来分析.其实试想一下namesrv的功 ...