Protecting the Flowers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7812   Accepted: 3151

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.

Source

 
 
题意:农夫John养的植物被cow吃了(John和cow的日常。。)现在给我们n组数,每组数分别表示赶走cow的单程时间和该cow每单位时间吃掉的植物数,求怎样赶cow使得损失最少。
思路:贪心。先赶走的cow一定是当前牛群里,用时尽可能少,吃得尽可能多的一头,但不一定是最少和最多。所以可以把t和d以比值的方式记录下来(性价比),这样就可以同时把t、d作为参数来考虑。每次先赶当前t/d最小的,使得总损失最小。
 
 
#include<stdio.h>
#include<algorithm>
using namespace std; struct Node{
double t,d;
}node[];
bool cmp(Node a,Node b)
{
return (a.t/a.d)<(b.t/b.d); //贪心
}
int main()
{
int n,i;
long long sum,ans;
scanf("%d",&n);
sum=;
for(i=;i<=n;i++){
scanf("%lf%lf",&node[i].t,&node[i].d);
sum+=node[i].d;
}
sort(node+,node+n+,cmp);
ans=;
for(i=;i<=n;i++){
sum-=node[i].d;
ans+=sum*node[i].t*;
}
printf("%lld\n",ans);
return ;
}

POJ 3262 Protecting the Flowers 贪心(性价比)的更多相关文章

  1. poj 3262 Protecting the Flowers 贪心 牛吃花

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11402   Accepted ...

  2. poj -3262 Protecting the Flowers (贪心)

    http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛 ...

  3. poj 3262 Protecting the Flowers 贪心

    题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草 求把所有奶牛送回牛棚内,所消耗草的最小值 思路:贪心,假设奶牛a和奶牛 ...

  4. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  5. POJ 3262 Protecting the Flowers 【贪心】

    题意:有n个牛在FJ的花园乱吃.所以FJ要赶他们回牛棚.每个牛在被赶走之前每秒吃Di个花朵.赶它回去FJ来回要花的总时间是Ti×2.在被赶走的过程中,被赶走的牛就不能乱吃 思路: 先赶走破坏力大的牛假 ...

  6. POJ 3362 Protecting the Flowers

    这题和金华区域赛A题(HDU 4442)是一样的做法. 对两个奶牛进行分析,选择两个奶牛总花费少的方式排序. bool cmp(const X&a,const X&b){ return ...

  7. 【POJ - 3262】Protecting the Flowers(贪心)

    Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...

  8. POJ3262 Protecting the Flowers 【贪心】

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4418   Accepted: ...

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

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

随机推荐

  1. pyquery模块

    pyquery 这个模块基本是仿JQuery的形式,也支持CSS选择器语法,因此对于爬虫来说,避免了正则表达式的滥用. 创建对象 from pyquery import PyQuery as pq d ...

  2. 1069: [SCOI2007]最大土地面积

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2961  Solved: 1162[Submit][Sta ...

  3. 解析json的方式

    一.Javascrip操作json 原始方式: var str1 = '{ "name": "jacun", "addr": "b ...

  4. js 单例模式的实现方式----闭包和构造函数内部判断

    闭包: var singleton = function( fn ){ var result; return function(){ return result || ( result = fn .a ...

  5. cc、gcc、g++区别

    gcc是C编译器:g++是C++编译器:linux下cc一般是一个符号连接,指向gcc:gcc和g++都是GUN(组织)的编译器.而CC则一般是makefile里面的一个名字,即宏定义,嘿,因为Lin ...

  6. Mac端博客发布工具推荐

    引子 推荐一款好用的 Mac 端博客发布工具. 下载地址 echo 博客对接 这里以cnblog为例.接入类型为metawebblog,access point可以在cnblog的设置最下边找到,然后 ...

  7. 原来 Set 集合也可以排序

    Java 集合类主要由两个接口派生而出: Collection 和 Map.在 Collection 集合中,我们经常用到的是 List 集合和 Map 集合,而 Set 集合出场的机会就相对比较的少 ...

  8. 基于BASYS2的VHDL程序——数字钟(最终版)

    转载请注明原地址:http://www.cnblogs.com/connorzx/p/3674178.html 调时电路正常工作.一切正常.发现做FPGA还是得从数电的思路思考,设置一个预置使能端,预 ...

  9. 矩阵管理——和visitor模式没有本质区别,都是为了避免资源重复

    矩阵管理中的员工是双线汇报的模式.其上司有两个,一个是流程上司,一个是专业上司.流程上司负责你的日常考核,专业上司负责你的晋升和任免. 管理条件 相对于矩阵管理的矩阵式组织,适合于某些较为庞大的全球性 ...

  10. UniCode转码

    <script type="text/javascript"> var GB2312UnicodeConverter = { ToUnicode: function ( ...