http://poj.org/problem?id=3262

Protecting the Flowers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4930   Accepted: 1965

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.
 
分析:
就是结构体排序,单位时间内吃花量。
 
AC代码:
 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<stack>
#include<map>
#include<string>
#define LL long long
using namespace std;
struct Node{
LL x, y;
bool operator < (const Node &A) const{
return y*1.0/x*1.0 > A.y*1.0/A.x*1.0;
}
}node[];
int main(){
int n;
while(~scanf("%d", &n)){
memset(node, , sizeof(node));
LL sumx = , sumy = ;
for(int i = ; i < n; i++){
scanf("%lld%lld", &node[i].x, &node[i].y);
sumx += node[i].x;
sumy += node[i].y;
}
sort(node, node+n);
LL ans = ;
for(int i = ; i < n; i++){
ans += (sumy -= node[i].y) * node[i].x * 2.0;
}
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: 7812   Accepted: ...

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

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

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

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

  4. poj 3262 Protecting the Flowers 贪心

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

  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. BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 448  So ...

  9. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )

    考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...

随机推荐

  1. Apple Pay--iOS开发

    一.什么是Apple Pay? 1. 概念 Apple Pay,简单来说, 就是一种移动支付方式.通过Touch ID/ Passcode,用户可使用存储在iPhone 6, 6p等设备上的信用卡和借 ...

  2. DB2常用命令

    DB2安装启动服务中启动不了,可用command启动并查看windows系统的日志.1.启动数据库  db2start2.停止数据库  db2stop3.连接数据库运行 db2命令之前要先运行db2c ...

  3. redis使用watch完成秒杀抢购功能

    Redis使用watch完成秒杀抢购功能: 使用redis中两个key完成秒杀抢购功能,mywatchkey用于存储抢购数量和mywatchlist用户存储抢购列表. 它的优点如下: 1. 首先选用内 ...

  4. Python之路第一课Day3--随堂笔记(文件操作)

    一.集合的介绍 1.集合操作 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 常用操作 s = se ...

  5. spark 2.0 Vector toBreeze

    def toBreeze( _v : Vector ) : BZV[Double] = { _v match { case x : org.apache.spark.mllib.linalg.Dens ...

  6. poj1700-Crossing River(贪心算法)

    一,题意: 只有一艘船,能乘2人,船的运行时间为2人中较多一人的时间,过去后还需一个人把船划回来,问把n个人运到对岸,最少需要多久.二,思路步骤: 想办法先把用时最多的2人,运过去,再把剩下来的人中用 ...

  7. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

  8. php上传大文件时,服务器端php.ini文件中需要额外修改的选项

    几个修改点: 1.upload_max_filesize 上传的最大文件 2.post_max_size 上传的最大文件 3.max_execution_time 修改为0表示无超时,一直等待 4.m ...

  9. [uva12170]Easy Climb

    还是挺难的一个题,看了书上的解析以后还是不会写,后来翻了代码仓库,发现lrj又用了一些玄学的优化技巧. #include <algorithm> #include <iostream ...

  10. require.js工作原理(初始)

    详情:请见阮一峰老师的日志:http://www.ruanyifeng.com/blog/2012/11/require_js.html: 导入:<script data-main=" ...