【bzoj1634】[Usaco2007 Jan]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 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.
输入
* 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.
输出
* Line 1: A single integer that is the minimum number of destroyed flowers
一个整数,表示最小数量的花朵被吞食.
样例输入
6
3 1
2 5
2 3
3 2
4 1
1 6
样例输出
86
题解
贪心
贪心策略:优先选择t/d小的。
略证明一下,假设有1和2,那么先运1的代价为2*t1*d2,先运2的代价为2*t2*d1。
想让代价1小于代价2,则2*t1*d2<2*t2*d1。
即t1/d1<t2/d2。
排好序后还要用到一个前缀和,我这个前缀和是反过来计算的(后缀和),应该也不难理解。
#include <cstdio>
#include <algorithm>
using namespace std;
struct data
{
int t , d;
}a[100005];
long long sum[100005];
bool cmp(data a , data b)
{
return a.t * b.d < a.d * b.t;
}
int main()
{
int n , i;
long long ans = 0;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ )
scanf("%d%d" , &a[i].t , &a[i].d);
sort(a + 1 , a + n + 1 , cmp);
for(i = n ; i >= 1 ; i -- )
sum[i] = sum[i + 1] + a[i].d;
for(i = 1 ; i <= n ; i ++ )
ans += 2 * sum[i + 1] * a[i].t;
printf("%lld\n" , ans);
return 0;
}
【bzoj1634】[Usaco2007 Jan]Protecting the Flowers 护花 贪心的更多相关文章
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
- BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 448 So ...
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )
考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
- 1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 493 So ...
- 【BZOJ】1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1634 贪心.. 我们发现,两个相邻的牛(a和b)哪个先走对其它的牛无影响,但是可以通过 a的破坏花× ...
- BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634 题意: 约翰留下他的N只奶牛上山采木.可是,当他回来的时候,他看到了一幕惨剧:牛们正 ...
- bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花【贪心】
因为交换相邻两头牛对其他牛没有影响,所以可以通过交换相邻两头来使答案变小.按照a.t*b.f排降序,模拟着计算答案 #include<iostream> #include<cstdi ...
- 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 ...
随机推荐
- 线上CPU飚高(死循环,死锁...)
之前排除服务器内存暴增的问题,在此看到一篇类似的文章,做个类似的记录. 1.top基本使用 top 命令运行图: 第一行:基本信息 第二行:任务信息 第三行:CPU使用情况 第四行:物理内存使用情况 ...
- 北京Uber优步司机奖励政策(12月31日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(1月13日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- spring源码-国际化-3.5
一.国际化在实际代码中是非常常见的一中方式.为了结合web做一下语言上面的切换,而达到展示的目的. 二.这里呢,主要是介绍spring中对于国际化做了哪些处理. 三.实现方式 1)xml配置 < ...
- 三、并行流与串行流 Fork/Join框架
一.并行流概念: 并行流就是把一个内容分成多个数据块,并用不同的线程分别处理每个数据块的流. java8中将并行进行了优化,我们可以很容易的对数据进行并行操作.Stream API可以声明性的通过pa ...
- CentOS6.5进不去系统,修复
今天进系统出现问题了,然后在网上搜索了一下解决方案解决了,把解决方法记录下来,方便以后查阅. 输入root密码 #mount | grep "on /" //得到root用户所在分 ...
- Oracle DELETE和TRUNCATE 的区别
语法delete from aa truncate table aa 区别 1.delete from后面可以写条件,truncate不可以. 2.delete from记录是一条条删的,所删除的每行 ...
- 编译chromium时下载gn.exe时出错的解决方案
天朝人写个代码真难,想要编译一下chromium,但是获取代码时各种坑,不是网速慢,就是网络联不通,真难玩. 本文针对下载gn.exe等工具时失败的解决方案. 原因1:gclient没有走代理,针对使 ...
- Python的包(Packages)
包,Package,是一种Python模块的集合,从文件组织形式上看,包就是一个文件夹,里面放着各种模块(.py文件),也可以有子文件夹(子包).包名构建了一个Python模块的命名空间.比如,模块名 ...
- LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告
原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...