POJ3262 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 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
Lines 2..N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow's characteristics
Output
Sample Input
6
3 1
2 5
2 3
3 2
4 1
1 6
Sample Output
86
Hint
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.
Source
被long long坑了6次...贪心方法为每次选择的牛使得剩下的牛对花园破坏最小。
证明:若a,b为即将选择的两头牛,若果先选a的话。b牛的破坏是b.d*a.t,若先选b的话。a的破坏是a.d*b.t;无论a,b谁先谁后,其余牛的破坏都是(a.t+b.t)*other.d,所以仅仅须要依照先选择的牛使得剩下的牛对花园的破坏最小的标准对牛排序,再逐个选择就是了。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm> #define maxn 100005
typedef long long LL; struct Node {
int t, d;
friend bool operator<(const Node& a, const Node& b) {
return a.t * b.d < b.t * a.d;
}
} cow[maxn]; int main() {
int N, i, sumt = 0;
LL sumd = 0;
scanf("%d", &N);
for(i = 0; i < N; ++i)
scanf("%d%d", &cow[i].t, &cow[i].d);
std::sort(cow, cow + N);
for(i = 0; i < N; ++i) {
sumd += cow[i].d * sumt;
sumt += cow[i].t * 2;
}
printf("%lld\n", sumd);
return 0;
}
POJ3262 Protecting the Flowers 【贪心】的更多相关文章
- 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 贪心
题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草 求把所有奶牛送回牛棚内,所消耗草的最小值 思路:贪心,假设奶牛a和奶牛 ...
- poj3262 Protecting the Flowers
思路: 简单贪心,每次选择性价比最高的. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...
- poj -3262 Protecting the Flowers (贪心)
http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛 ...
- 【POJ - 3262】Protecting the Flowers(贪心)
Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )
考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
随机推荐
- 用list去初始化numpy的array数组 numpy的array和python中自带的list之间相互转化
http://blog.csdn.net/baiyu9821179/article/details/53365476 a=([3.234,34,3.777,6.33]) a为python的list类型 ...
- delphi jinchengneicun
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Configuring_the_Memory_Manager https://docs.micros ...
- spring注解开发-AOP(含原理)
一:AOP基本使用 AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式: 步骤一:导入aop模块:Spring AOP:(spring-aspects) <depen ...
- UVa-401-Palindromes(回文)
这一题的话我们可以把映像字符的内容给放入一个字符串常量里面,然后开辟一个二维的字符串常量数组,里面放置答案. 对于回文实际上是很好求的,对于镜像的话,我们写一个返回char的函数,让它接收一个char ...
- 【struts2】学习笔记
常见问题及注意事项: 1.下载struts2时,要看清所下载的版本,不同版本web.xml配置路径不同! 2. 导入jar包时,导入的包要完全准确,缺少或过多的会导致缺失或冲突! 3. Registe ...
- 微信小程序的坑之wx.miniProgram.postMessage
工作中有个需求是小程序的网页在关闭的时候,需要回传给小程序一个参数 查阅小程序官方文档,有这样一个接口 wx.miniProgram.postMessage ,可以用来从网页向小程序发送消息,然后通过 ...
- pycharm的快捷键以及常用设置
1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 快速导入任意类 Ctrl + Shift + Enter 语句完成 Ctr ...
- python-基本运算符(解压缩-必考)
基本运算符 算术运算符 x =10 y =20 print(x+y) 30 print(x-y) -10 print(x*y) 200 print(x/y) 0.5 print(x%y)#取余 10 ...
- 大数据学习——mapreduce汇总手机号上行流量下行流量总流量
时间戳 手机号 MAC地址 ip 域名 上行流量包个数 下行 上行流量 下行流量 http状态码 1363157995052 13826544101 5C-0E-8B-C7-F1-E0:CMCC 12 ...
- POJ3246-Balanced Lineup,好经典的题,做法和HDU-I hate it 一样~~
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Case Time Limit: 2000MS Description For ...