【贪心算法】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 ...
随机推荐
- mysql数据库使用insert语句插入中文数据报错
在mysql的命令行模式中,通过insert语句插入中文数据的时候报错,类似于下面这样: Incorrect string value: '\xE7\x8F' for column 'name' at ...
- Android ViewPager设置监听注意事项
首先 implements View.OnClickListener 因为Item比较多用这个方便 设置监听要注意地方,如果在 onCreate 直接 findViewById布局里的ID是会出错的 ...
- python基础学习1-面向对象
#!/usr/bin/env python # -*- coding:utf-8 -*- class Foo:#定义类 def mail(self,email,message):#定义类的方法 pri ...
- 如何用 Python 爬取需要登录的网站
[原文地址:]http://python.jobbole.com/83588/ import requests from lxml import html # 创建 session 对象.这个对象会保 ...
- 二维码PDF417简介及其解码实现(zxing-cpp)
二维码PDF417是一种堆叠式二维条码.PDF417条码是由美国SYMBOL公司发明的,PDF(Portable Data File)意思是"便携数据文件".组成条码的每一个条码字 ...
- Eclipse中Applet程序运行时Applet小程序大小的设置
最近在跟斯坦福的CS106A,里面的java代码都是Applet程序,而我运行程序的时候发现,Applet小程序窗口大小总是固定的,但是我画的图却越来越大,所以怎么在Eclipse中run的时候 ...
- 菜鸟vimer成长记——第1章、统一概念
不管学什么技术,我都深信概念是最重要的.是影响整个学习轨迹,决定能在这个技术领域高度. 当然如果你现在的目的不是在学习而在于解决问题(很多人不愿意承认,或者没发现),那概念就暂时没那么重要了. 目的 ...
- c#简易学生信息管理系统
在近期的学习中,我们学习了泛型及泛型集合的概念和使用,泛型是c#中的一个重要概念,为了巩固我们学习的成果,我们可以使用一个实例来进行练习 题目及要求 要求使用Windows窗体应用程序,制作出如上图的 ...
- Unity学习笔记(3):一些常用API和应用场景
Mathf.Lerp(float a,float b,float t)插值函数,当a < b时往a中插入t,以此来实现颜色,声音等渐变效果. GameObject.FindWithTag(str ...
- mac zsh不自动加载~/.bashrc
修改了bashrc, 新开一个终端都要source一下才起作用. 网上有说需要在 . bash_profile加载一次.bashrc. 但是这个和我的问题不一样. 我用的是zsh,需要修改~/.zsh ...