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 ...
随机推荐
- 产生多种anchor的代码讲解!很好!
http://blog.csdn.net/xzzppp/article/details/52317863 源代码:https://github.com/rbgirshick/py-faster-rcn ...
- DLL动态链接库的创建
dll的创建主要有两种方法:一是使用 __declspec(dllexport) 创建dll,二是使用模块定义(.def)文件创建dll. 使用 __declspec(dllexport) 创建dll ...
- 【原创翻译】链接DLL至可执行文件---翻译自MSDN
可执行文件.exe链接(或加载)DLL有以下两种形式: 隐式链接 显式链接 隐式链接是指静态加载或在程序加载时动态链接. 通过隐式链接,在使用DLL时,可执行文件链接到一个由生成DLL的人提供的导入函 ...
- xshell通过xftp传输Windows文件到Linux:在输入put后,再摁 TAB 键,可显示当前文件夹的文件
在输入put后,再摁 TAB 键,可显示当前文件夹的文件 sftp:/home/yan> put $Recycle.Bin\ BluestacksCN\ ...
- Openjudge-4151-电影节
这个题是一道贪心的题目,我们要想看的电影数目最多,我们肯定每次都要选最早结束的电影,这样我们才能去看下一部电影. 它本身最早结束,如果同时开始,那肯定是它的放映时间比较短,如果它后开始,先结束,那它的 ...
- 数组合并--php
常用的合并数组方法有以下几种: 1 array_merge 2 '+' 3 array_merge_recursive 下面是一段对比的代码 $array1 = array(2,4," ...
- linux 服务器 php vue项目部署流程总结
服务器配置 购买阿里云服务器 (选择ubuntu 16系统 / 内存2G以上) 安全策略, 入规则: 添加端口 20,21,22, 80, 443, 3306, 8080, 安装宝塔 wget -O ...
- ES6(Proxy 和 Reflect)
Proxy 和 Reflect 1.Proxy 和 Reflect 的概念 Proxy 意为 ‘代理’,连接了用户和真实对象之间的一个层 Reflect 意为‘反射’ 反射的是Object 2.适 ...
- Java基础学习总结(91)——阿里巴巴Java开发手册公开版
1.不要嫌名字长 无论是方法,变量,还是函数的取名,不要嫌弃名称太长,只要能够表示清楚含义就可以了. 2.String[] args而不是String args[] 中括号是数组类型的一部分,数组定义 ...
- [转]Selenium-Webdriver系列Python版教程(1)————快速开始
elenium的历史,selenium2与WebDriver的关系本文就不讲了,想了解的同学们百度一下就可以Ok. 本系列教程是以Selenium-WebDriver的Python版本,首先从 ...