POJ:3262-Protecting the Flowers
Protecting the Flowers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8606 Accepted: 3476
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 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.
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
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
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头牛在吃花,每头牛牵走(避免吃花)要花费2*t秒(来回),每头牛吃花的速率是d,要你自己设计一个牵走牛的顺序,使被牛吃的花最少。
- 假设有两头牛分别编号1,2,1号牛牵走需要花费x1秒,吃花的速率是y1,2号牛牵走需要x2秒,吃花的速率是y2,假设先牵走1号牛,那么2号牛将要吃y2*x1数量的花,如果先牵走2号牛,那么1号牛要吃y1*x2数量的花,假设二号牛吃得花比1号牛多,那么就有关系式y2*x1>y1*x2,那么可以移项得到y2/x2>x2/x1,所以这样就可以按照这个速率比时间的值来贪心,让被牛吃的花尽量少。
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
struct NODE {
ll d,t;
double v;
}cow[maxn];
bool cmp(NODE a, NODE b) {
return a.v < b.v;
}
int main() {
ll n,t = 0,ans = 0;
scanf("%lld",&n);
for(int i=0;i<n;i++){
scanf("%lld%lld",&cow[i].t,&cow[i].d);
cow[i].v = (double)cow[i].t/(double)cow[i].d;
}
sort(cow,cow+n,cmp);
for(int i=0;i<n;i++) {
ans += cow[i].d * t;
t += cow[i].t*2;
}
printf("%lld\n",ans);
return 0;
}
POJ:3262-Protecting the Flowers的更多相关文章
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
- POJ 3262 Protecting the Flowers 贪心(性价比)
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7812 Accepted: ...
- poj 3262 Protecting the Flowers 贪心 牛吃花
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11402 Accepted ...
- poj -3262 Protecting the Flowers (贪心)
http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛 ...
- poj 3262 Protecting the Flowers 贪心
题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草 求把所有奶牛送回牛棚内,所消耗草的最小值 思路:贪心,假设奶牛a和奶牛 ...
- POJ 3262 Protecting the Flowers 【贪心】
题意:有n个牛在FJ的花园乱吃.所以FJ要赶他们回牛棚.每个牛在被赶走之前每秒吃Di个花朵.赶它回去FJ来回要花的总时间是Ti×2.在被赶走的过程中,被赶走的牛就不能乱吃 思路: 先赶走破坏力大的牛假 ...
- 【POJ - 3262】Protecting the Flowers(贪心)
Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...
- 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 , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
随机推荐
- PHP underlying structure
http://www.phpinternalsbook.com/classes_objects/magic_interfaces_comparable.html
- Oracle数据表比较记录差异(转)
liuyx_know|七级 你可以参照一下Oracle的UNION [ALL], INTERSECT, MINUS操作符,至于你的问题你可以使用MINUS操作符,语句如下: SELECT * FROM ...
- 用Android studio进行 OpenCV 开发的第一个项目
我的天! 折腾了好久终于搭建成功了第一个项目. 项目环境: Windows 7 家庭普通版 64位 Android studio 1.5.1 OpenCV-2.4.9-android-sdk 基于 ...
- python socket练习
服务器端 #服务器端 import socket server = socket.socket() server.bind(('localhost',6969))#绑定要监听的端口 server.li ...
- 网页打开速度优化——HTTP请求头及响应头
no-cache:不缓存过期的资源 no-store:不缓存 最近看了<图解HTTP>这本书,书上讲到了这两者的区别: no-cache从字面意义上很容易误解为不缓存,但是no-cache ...
- Docker cgroup.procs no space left on device
环境:centos6 运行docker 时 错误提示: System error: write /sys/fs/cgroup/docker/01f5670fbee1f6687f58f3a943b1e1 ...
- Jmeter入门7 测试中使用到的几个定时器和逻辑控制器
1 测试中提交数据有延时1min,所以查询数据是否提交成功要设置定时器. 固定定时器页面:单位是毫秒 [dinghanhua] 2 集合点.Synchronizing Timer 集合点编辑:集合用户 ...
- C++学习之显式类型转换与运行时类型识别RTTI
static_cast const_cast reinterpret_cast 运行时类型识别(RTTI) dynamic_cast 哪种情况下dynamic_cast和static_cast使用的情 ...
- DEM精度评价自动化系统的成果展示
程序员:左正康 完成时间:2013/12/3 系统开发背景: 原始的DEM精度评价方法:采用ArcGIS结合Excel的方式完成DEM的精度评价.具体操作是:利用ArcGIS工具箱中的创建TIN,T ...
- 使用shell脚本实现在liunx上进行svn的上传下载更新功能
最近有个功能,是需要从在liunx上拉取svn地址,并创建一个新文件进行提交,shell脚本如下 #!/bin/bash echo "Hello World !" myFile=& ...