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朵 ...
随机推荐
- c++命名空间using
#include<iostream> namespace run1 { ; } namespace run2 { ; void show() { std::cout << x ...
- <php>文件操作*(重要)
//touch("./3.txt");//创建文件:在当前目录下创建3.txt文件 //copy("./3.txt","./touxiang/5.ph ...
- Laravel-高级篇-Auth-数据迁移-数据填充
(慕课网_轻松学会Laravel-高级篇_天秤vs永恒老师_http://www.imooc.com/learn/702) 一.生成Auth所需文件 在Artisan控制台输入以下命令 php art ...
- sql加强练习
1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 76李四 数学 90王五 语文 81王五 数学 100王五 ...
- MongoDB 的 MapReduce 大数据统计统计挖掘
MongoDB虽然不像我们常用的mysql,sqlserver,oracle等关系型数据库有group by函数那样方便分组,但是MongoDB要实现分组也有3个办法: * Mongodb三种分组方式 ...
- [Redux] Passing the Store Down with <Provider> from React Redux
Previously, we wrote the Provider component by ourself: class Provider extends Component { getChildC ...
- JavaScript总结(二) 系统分析
学习Javascript的时候.遇到了一个问题. 看了牛腩的视频,感觉讲的太简单和基础,像认识一位新朋友.但仅有一面之缘就结束了. 再看姜浩的视频.又看得迷迷糊糊,让我找到了当初看两位台湾专家讲VB. ...
- 私人C#笔记
coust 定义常量 string是密封类,所以不能继承它 namespace默认是按照文件夹的结构命名的,如(System.文件夹.子文件夹),而且namespace是可以手动改的 Arra ...
- javascript-Blob文件对象
一个Blob对象就是一个包含有只读原始数据的类文件对象.Blob对象中的数据并不一定得是JavaScript中的原生形式.File接口基于Blob,继承了Blob的功能,并且扩展支持了用户计算机上的本 ...
- macbook安装mysql
一.官网下载dmg文件 二.双击安装dmg文件,一路next: 三.