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.
 
思路:贪心问题,吃草效率最高的先选(D/T),代码如下:
typedef long long LL;

const int maxm = ;

struct Node {
int T, D;
bool operator< (const Node &a) const {
return (a.D * 1.0) / a.T < D * 1.0 / T;
}
} Nodes[maxm]; int n;
LL times = , sum = ; int main() {
scanf("%d", &n);
for (int i = ; i < n; ++i) {
scanf("%lld%d", &Nodes[i].T, &Nodes[i].D);
}
sort(Nodes, Nodes + n);
for(int i = ;i < n; ++i) {
sum += Nodes[i].D * times;
times += Nodes[i].T * ;
}
printf("%lld\n", sum);
return ;
}

Day3-D-Protecting the Flowers POJ3262的更多相关文章

  1. POJ3262 Protecting the Flowers 【贪心】

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

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

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

  3. poj 3262 Protecting the Flowers

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

  4. BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花

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

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

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

  6. 1634: [Usaco2007 Jan]Protecting the Flowers 护花

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

  7. bzoj1634 / P2878 [USACO07JAN]保护花朵Protecting the Flowers

    P2878 [USACO07JAN]保护花朵Protecting the Flowers 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...

  8. [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心

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

  9. 洛谷——P2878 [USACO07JAN]保护花朵Protecting the Flowers

    P2878 [USACO07JAN]保护花朵Protecting the Flowers 题目描述 Farmer John went to cut some wood and left N (2 ≤ ...

  10. POJ 3262 Protecting the Flowers 贪心(性价比)

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

随机推荐

  1. springboot:配置多个数据源

    参考:还未整理 https://www.cnblogs.com/carrychan/p/9401471.html https://www.cnblogs.com/lijianda/p/11022892 ...

  2. 23 JavaScript规范与最佳实践&性能&箭头函数

    大多数web服务器(Apache等)对大小写敏感,因此命名注意大小写 不要声明字符串.数字和布尔值,始终把他们看做原始值而非对象,如果把这些声明为对象,会拖慢执行速度 对象是无法比较的,原始值可以 不 ...

  3. 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(4)

    # -*- coding: utf-8 -*- import glob import os.path import numpy as np import tensorflow as tf from t ...

  4. DMVPN基础配置

    DMVPN基础拓扑: 配置步骤:    1. 基本IP地址配置实现网络可达    2. 配置GRE多点隧道(mGRE)和NHRP(下一跳解析协议)    3. 配置EIGRP路由协议    4. 配置 ...

  5. 卷积神经网络(CNN)_相关知识

    斯坦福公开课 CS231n Convolutional Neural Networks for Visual Recognition : http://cs231n.stanford.edu/syll ...

  6. 【Hibernate 一对多】

    OneToMany public class OneToMany { @Test public void testAdd1() { SessionFactory sessionFactory = nu ...

  7. Java连载66-数组的两种初始化方式

    一.数组 1.数组中存储元素的类型是统一的,每一个元素在内存中所占用的空间大小是相同的,知道数组的首元素的内存地址,要查找的元素只要知道下标,就可以快速的计算出偏移量,通过首元素内存地址加上偏移量,就 ...

  8. 10 从DFS到DFT

    从DFS到DFT 周期序列的级数展开 正如连续时间周期信号可以表示为一系列正弦信号的和的形式,周期序列也可以表示为一系列正弦之和的形式,假设序列\(\tilde{x}[n]\)的周期为\(N\),那 ...

  9. ShellCode模板

    前言 在上一篇文章上使用到的添加用户的shellcode是怎么得到的呢? 先来拆分一下汇编的功能 ;寻找kernel32.dll的基地址 xor ecx,ecx mov eax,dword ptr f ...

  10. 第九届蓝桥杯B组决赛 调手表(完全背包)

    问题描述 M78 星云的一个小时有 n 分钟. 大家都知道,手表只有一个按钮可以把当前的数加一.在调分钟的时候,如果当前显示的数是 0 ,那么按一下按钮就会变成 1,再按一次变成 2 .如果当前的数是 ...