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. ok6410[001] Ubuntu 16.04[64bit]嵌入式交叉编译环境arm-linux-gcc搭建过程图解

    开发PC:Ubuntu16.04.1 开发板:OK6410[飞凌公司出品] 目标:通过GPIO点亮LED ----------------------------------------------- ...

  2. fedora delete openJDK

    博客分类: linux   由于Fedora系统安装的时候会自带OpenJDK,安装完系统后 java -version 会显示  [root@localhost bin]# java -versio ...

  3. POSIX标准中的 “ 限制 ”

    前言 在POSIX标准中,定义了许多限制.这些限制大约分为五类,不同类型的限制获取的方式不一样. 限制值分类 1. 不变的最小值 这类型的限制值是静态的,固定的. 2. 不变值 同上 3. 运行时可以 ...

  4. 数据结构基础之memset---有memset 抛出的int 和 char 之间的转换和字节对齐

    今天晚上,在做滤波算法时,里面用到很多float 和int 以及char 之间的类型强制转换,后面滤波完发现图片有些区域块,有过度曝光的白光,我就跟踪,以为是char 字符数字数据溢出问题,加了0-2 ...

  5. 图像处理之opencv---加减乘除运算cvdiv

    http://chyyeng.blog.163.com/blog/static/16918230201211632135456/

  6. Spring Data MongoDB 四:基本文档改动(update)(一)

    Spring Data MongoDB 三:基本文档查询(Query.BasicQuery)(一) 学习MongoDB 二:MongoDB加入.删除.改动 一.简单介绍 Spring Data  Mo ...

  7. c# SQLServer导入大批量数据

    说来惭愧,关于批量导入数据,一直采用的是最原始的方式,一条一条插入,或者100条一块批量插入,这种方式,五十步笑百步,并没有明显的性能提升, 昨天在从别的库查询数据到DataTable内存中,然后插入 ...

  8. wince c# 创建桌面快捷方式 .

    static void Create() { string PathGPRS = System.IO.Path.GetDirectoryName(System.Reflection.Assembly. ...

  9. 【BZOJ2338】[HNOI2011]数矩形 几何

    [BZOJ2338][HNOI2011]数矩形 题解:比较直观的做法就是枚举对角线,两个对角线能构成矩形当且仅当它们的长度和中点相同,然后用到结论:n个点构成的矩形不超过n^2.5个(不会证),所以两 ...

  10. android studio发布公共类库到服务器maven仓库

    在上一篇中提到了怎么创建私有maven库,这篇主要结合android studio的使用,直接进入正题,看以下步骤 1.创建android项目 创建Project,然后加一个library的modul ...