一、题目

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

二、思路&心得

  • 贪心策略:用d/t的值进行选择,值大的数据先处理,以达到花费代价最小。
  • 可以使用排序或则优先队列做,个人觉得排序比较简单直接。至于每一次数据处理时的代价计算,利用总代价反复减去对应数据的代价即可。
  • 题目需注意是数据量较大,所有结果应采用long long或则__int64类型进行定义。

三、代码

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N = 100005; typedef pair<float, float> P; int N, sum; long long cost; P a[MAX_N]; bool cmp(P a, P b) {
return a.second / a.first > b.second / b.first;
} void solve() {
sum = 0, cost = 0;
for (int i = 0; i < N; i ++) {
scanf("%f %f", &a[i].first, &a[i].second);
sum += a[i].second;
}
sort(a, a + N, cmp);
for (int i = 0; i < N; i ++) {
sum -= a[i].second;
cost += 2 * a[i].first * sum;
}
printf("%lld\n", cost);
} int main() {
while (~scanf("%d", &N)) {
solve();
}
return 0;
}

【贪心算法】POJ-3262的更多相关文章

  1. poj 1088 滑雪(贪心算法)

    思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...

  2. POJ 2287 田忌赛马 贪心算法

    田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...

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

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

  4. poj_1042 贪心算法

    poj 1042 gone fishing 题目要求: 由有n个湖, 按照顺序排列,一个人从第一个湖向最后一个湖行进(方向只能从湖0到湖n-1),途中可以在湖中钓鱼.在每个湖中钓鱼时,开始的5分钟内可 ...

  5. poj_2709 贪心算法

    poj 2709 painter 题目要求 给定涂料,每套涂料含有3-12种不同的颜色(开始时候给定选用的颜料套的颜色数目),且一套涂料中每种颜色均有50ml.且一套涂料中的任意三种不同的颜色各X m ...

  6. ACM 贪心算法总结

    贪心算法的本质: 就是当前状态的最优解,它并不考虑全局. 什么是当前状态的最优解? 成本问题? https://www.cnblogs.com/xuxiaojin/p/9400892.html (po ...

  7. 贪心算法(Greedy Algorithm)

    参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...

  8. 算法导论----贪心算法,删除k个数,使剩下的数字最小

    先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...

  9. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  10. ACM_ICPC hdu-2111(简单贪心算法)

    一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...

随机推荐

  1. 实现一个自定义event事件,包括on ,off,trigger,once

    on监听事件,off取消事件 ,trigger触发事件,once只执行一次 class Event { constructor() { this.handlers = {};//记录所有的事件以及处理 ...

  2. iOS 开发之UIStackView的应用

    ————————————————UIStackView的应用———————————————— 一:先讲下优势: 对于排布列表式控件的布局需求,用UIStackView控件,开发中为我们省去了繁琐的代码 ...

  3. 【转】netty源码分析之LengthFieldBasedFrameDecoder

    原文:https://www.jianshu.com/p/a0a51fd79f62 拆包的原理 关于拆包原理的上一篇博文 netty源码分析之拆包器的奥秘 中已详细阐述,这里简单总结下:netty的拆 ...

  4. 可用的ntp服务器

    操作系统中带的:time.windows.com 和 time.nist.gov  网上查到一个公共的:cn.ntp.org.cn 以上三个连接多次才成功一次,速度不好. 在移动电视盒子上有一个配置: ...

  5. WPF实现MDI窗体的方法

    原文:WPF实现MDI窗体的方法 第一:新建一个类(Class) Win32Native.cs 代码如下: using System;  using System.Collections.Generi ...

  6. 洛谷 1440 求m区间内的最小值

    洛谷  1440 求m区间内的最小值 题目描述 一个含有n项的数列(n<=2000000),求出每一项前的m个数到它这个区间内的最小值.若前面的数不足m项则从第1个数开始,若前面没有数则输出0. ...

  7. 【转载】Free Lunch is Over(免费午餐已经结束了)

    原文:Free Lunch is Over(免费午餐已经结束了) 微软C++大师Herb Sutter的文章<The Free Lunch Is Over>翻译,以前自己也经常翻译,但是都 ...

  8. 【转载】OCX和DLL的区别

    原文:http://blog.csdn.net/scucj/article/details/852181 一.关于DLL的介绍      DLL,动态链接库,Dynamic Link Library的 ...

  9. node.js学习笔记——前序

    一.什么是node.js 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一 ...

  10. 私有Docker仓库login Error response from daemon: Get https://x.x.x.x/v2/: dial tcp x.x.x.x:443: connect: connection refused

    一.登陆私有仓库错误: docker login --username=evan 192.168.0.203 Error response from daemon: Get https://192.1 ...