poj 3262 Protecting the Flowers 贪心
题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草
求把所有奶牛送回牛棚内,所消耗草的最小值
思路:贪心,假设奶牛a和奶牛b所处位置为,
交换前 ....(ta, da) (tb, db)....
交换后 ....(tb, db) (ta, da)....
设此前已消耗的时间为x,那么交换前消耗的草:x*da + (x+ta)*db
交换后消耗的草:x*db + (x+tb)*da
除非交换后的消耗相比交换前的小才交换,即ta*db > tb*da
AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100005;
const int INF = 0X3f3f3f3f;
int n,t[N],d[N],sum,vis[N];
struct node
{
int t,d;
}w[N];
int cmp(node n1, node n2)
{
return n1.t*n2.d < n1.d * n2.t;
}
void solve()
{
sort(w,w+n,cmp);
long long ans = 0;
for(int i = 0; i < n; i++)
{
sum -= w[i].d;
ans += sum*w[i].t*2;
}
cout<<ans<<endl;
}
int main()
{
while(~scanf("%d", &n))
{
sum = 0;
for(int i = 0; i < n; i++)
scanf("%d %d", &w[i].t, &w[i].d),sum+=w[i].d;
solve();
}
return 0;
}
poj 3262 Protecting the Flowers 贪心的更多相关文章
- POJ 3262 Protecting the Flowers 贪心(性价比)
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7812 Accepted: ...
- poj 3262 Protecting the Flowers 贪心 牛吃花
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11402 Accepted ...
- poj -3262 Protecting the Flowers (贪心)
http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛 ...
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
- POJ 3262 Protecting the Flowers 【贪心】
题意:有n个牛在FJ的花园乱吃.所以FJ要赶他们回牛棚.每个牛在被赶走之前每秒吃Di个花朵.赶它回去FJ来回要花的总时间是Ti×2.在被赶走的过程中,被赶走的牛就不能乱吃 思路: 先赶走破坏力大的牛假 ...
- POJ 3362 Protecting the Flowers
这题和金华区域赛A题(HDU 4442)是一样的做法. 对两个奶牛进行分析,选择两个奶牛总花费少的方式排序. bool cmp(const X&a,const X&b){ return ...
- 【POJ - 3262】Protecting the Flowers(贪心)
Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...
- POJ3262 Protecting the Flowers 【贪心】
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4418 Accepted: ...
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
随机推荐
- PHP设计模式笔记七:观察者模式 -- Rango韩老师 http://www.imooc.com/learn/236
观察者模式 概述: 1.观察者模式(Observer),当一个对象状态发生改变时,依赖他的对象全部会收到通知,并自动更新 2.场景:一个事件发生后,要执行一连串更新操作,传统的编程方式,就是在事件的代 ...
- C++刷称号——2707: 素数与要素
Description 从键盘输入的随机整数n,如果n不是质数,然后计算n所有的因素(不含1).例如,对于16,出口2,4,8:否则输出"It is a prime number." ...
- Hibernate自增列保存失败的问题
author: hiu 更正说明:今天(2014-07-07)才发现的问题,我把@Id设置在了实体类中的id中,@Id是主键,应该设置在实体类的keyjobno中,之前发的文章可能误导了大家,如今更正 ...
- CSS结构伪类E:first-child/last-child/only-child/empty
E:first-child解释:E的父元素的第一个子元素正好是E,给这个E定义样式 E:last-child解释:E的父元素的最后一个子元素正好是E,给这个E定义样式 E:only-child解释:E ...
- HTML与CSS入门——第八章 使用外部和内部链接
知识点: 1.链接锚的使用方法 2.在自己的网站上的页面之间链接的方法 3.链接到外部内容的方法 4.链接到一个E-mail地址的方法 5.在新浏览器窗口中查看链接的方法 6.用CSS为链接添加样式的 ...
- poj 3104 二分
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12568 Accepted: 3243 Descripti ...
- 【译】神经网络与深度学习 Ch1-Section0
用神经网络识别手写数字 人类的视觉系统是是大自然的奇迹.考虑下面手写数字序列: 大多数人能够轻易地是识别出是504192.在我们大脑的每个半球都有一个基础的皮质,这就是我们熟知的V1区,它包含了14亿 ...
- android 点击水波纹效果
这里是重点,<ripple>是API21才有的新Tag,正是实现水波纹效果的; 其中<ripple android:color="#FF21272B" .... ...
- PHP第二课笔记
★Php的基本概念 快速入门案例 test.php <html> <body> //<?php ?>是运行在服务端 <?php echo 'hello' ...
- NAS4Free 安装配置(二)系统安装
NAS4Free系统安装 看一看BIOS设置 开机按DEL进BIOS 改日期时间 这里可以设置RAID,因为ZFS的RAID功能更好,所以我们在这里不配置RAID 制作LiveUSB 用软件(USB ...