【贪心算法】POJ-3262
一、题目
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的更多相关文章
- poj 1088 滑雪(贪心算法)
思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...
- POJ 2287 田忌赛马 贪心算法
田忌赛马,大致题意是田忌和国王赛马,赢一局得200元,输一局输掉200元,平局则财产不动. 先输入一个整数N,接下来一行是田忌的N匹马,下一行是国王的N匹马.当N为0时结束. 此题为贪心算法解答,有两 ...
- 【POJ - 3262】Protecting the Flowers(贪心)
Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...
- poj_1042 贪心算法
poj 1042 gone fishing 题目要求: 由有n个湖, 按照顺序排列,一个人从第一个湖向最后一个湖行进(方向只能从湖0到湖n-1),途中可以在湖中钓鱼.在每个湖中钓鱼时,开始的5分钟内可 ...
- poj_2709 贪心算法
poj 2709 painter 题目要求 给定涂料,每套涂料含有3-12种不同的颜色(开始时候给定选用的颜料套的颜色数目),且一套涂料中每种颜色均有50ml.且一套涂料中的任意三种不同的颜色各X m ...
- ACM 贪心算法总结
贪心算法的本质: 就是当前状态的最优解,它并不考虑全局. 什么是当前状态的最优解? 成本问题? https://www.cnblogs.com/xuxiaojin/p/9400892.html (po ...
- 贪心算法(Greedy Algorithm)
参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...
- 算法导论----贪心算法,删除k个数,使剩下的数字最小
先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...
- 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 ...
- ACM_ICPC hdu-2111(简单贪心算法)
一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...
随机推荐
- Redis数据类型基本操作
String类型: 设置键值对: set key value 设置键值对和过期时间:setex key seconds value ( 以秒为单位 ) 设置多个键值对: mset key1 value ...
- STM32中EXTI和NVIC的关系
(1)NVIC(嵌套向量中断):NVIC是Cortex-M3核心的一部分,关于它的资料不在<STM32的技术参考手册>中,应查阅ARM公司的<Cortex-M3技术参考手册>C ...
- 使用RT3070使开发板上网
原文地址:http://www.cnblogs.com/NickQ/p/8973880.html 使开发板上网 USB驱动部分 在arch/arm/mach-s3c2440/mach-smdk2440 ...
- 读取和修改app.config文件
本处主要是指读取和修改appSettings节点: 读取: string port = ConfigurationManager.AppSettings["port"]; //p ...
- inux下进程的最大线程数、进程最大数、进程打开的文件数
inux下进程的最大线程数.进程最大数.进程打开的文件数 2008-12-07 23:48 ========================= 如下转载自这里. linux 系统中单个进程的最大 ...
- 博弈论简单入门sb总结
博弈论简单入门sb总结 下午讲博弈论.没预习,GG. 整个下午都在学. 0 有一堆共n个石子,两个人轮流取石子,每个人一次可以取1到k个,取到最后一个石子的人胜利. 小学生都会的sb题.若k+1|n, ...
- python基础—字典
阅读文本需要3分钟,不建议跳读 节目清单 字典是python中最重要的数据类型,字典由“键-值”对组成的集合,字典中的“值”通过“键”来引用.这里将介绍字典的定义.访问.排序等功能. 字典的创建 字典 ...
- 经典笔试题:用C写一个函数测试当前机器大小端模式
“用C语言写一个函数测试当前机器的大小端模式”是一个经典的笔试题,如下使用两种方式进行解答: 1. 用union来测试机器的大小端 #include <stdio.h> union tes ...
- jmeter—操作数据库
添加JDBC Request,添加需要执行的sql语句 在这个界面需要配置Variabke Name,内容要与上表中的Name值相同:数据库的用户名.密码.URL.驱动这些基本信息要在这里配置:其他选 ...
- Jenkins服务器维护
Jenkins服务器维护 以下是一些基本的活动,一些是对 Jenkins 服务器维护的最佳实践 URL选项 在 Jenkins 实例 URL 有以下命令将开展对Jenkins实例的相关动作. http ...