Greedy:Protecting the Flowers(POJ 3262)

题目大意:就是农夫有很多头牛在践踏花朵,这些牛每分钟破坏D朵花,农夫需要把这些牛一只一只运回去,这些牛各自离牛棚都有T的路程(有往返,而且往返的时候这只牛不会再破坏花),问怎么运才能使被践踏的花最少?
一开始我做这道题的时候,用的是贪婪算法,然后我是这样做的,我先把D按照逆序排一遍,然后如果D相等的时候,再按T的顺序排序,然后两两比较看最小,就选谁,自以为是一个非常好的思路,但是果断WA了。
其实你要问我为什么这个思路?我一开始是这么想的,我想尽量让D大的元素出列,然后让T尽量少影响最后的结果,但是我没有注意到其实这个最小时间不仅仅是由D决定的,是T和D共同决定的成果,比如给一组这样的数据 4 :(2,1),(1,3),(5,7),(4,8),这个算法是选(4,8)(1,3),(5,7),(2,1)来得出最后结果为114,而其实还有更好的结果:先选(1,3),然后(4,8),(5,7),(2,1)结果为106,所以这个结果很明显就是错的
那么我们应该怎么办呢?我们看到,如果我们单独选择两个牛的时候,其实选完这两头牛,对后面是没有影响的(时间均为t1+t2),那么在这两头牛之间选择,所以其实我们只要看2*ta*DB和2*tb和DA之间的大小关系就可以了(DB+DA=SAB,所以在SAB里面而TA刚好就是对应DB,反之同理),进而我们只用看TA/DA和TB/DB的关系就可以了!!!这个区间思想拓展到整个区间就是我们要的算法我们按照TX/DX排顺序,最后出来的结果就是答案
#include <iostream>
#include <functional>
#include <algorithm> using namespace std; typedef struct set_
{
int T;
int D;
}Cows; void Search(const int,const int); int fcmop(const void *a, const void *b)
{
return (double)((*(Cows *)a).T) / (double)((*(Cows *)a).D) >
(double)((*(Cows *)b).T) / (double)((*(Cows *)b).D) ? : -;
}
static Cows Cows_Set[]; int main(void)
{
int Cows_sum, sum_D; while (~scanf("%d", &Cows_sum))
{
sum_D = ;
for (int i = ; i < Cows_sum; i++)
{
scanf("%d%d", &Cows_Set[i].T, &Cows_Set[i].D);
sum_D += Cows_Set[i].D;
}
/*为什么可以按照ea/排序?
*非常简单,因为当我们以两头牛两头牛选择的时候,我们会发现其实我们选完这两头牛的时候,后面的牛根本不受影响
*所以我们只要不断取2*ea*tb<2*eb*ta最小即可
*/
qsort(Cows_Set, Cows_sum, sizeof(Cows), fcmop);
Search(Cows_sum, sum_D);
}
return ;
} void Search(const int Cows_sum, const int sum_D)
{
long long lastA, j, sum_last = sum_D,ans = ; for (j = ; j < Cows_sum; j++)
{
sum_last-= Cows_Set[j].D;
ans += sum_last * * Cows_Set[j].T;
}
printf("%lld\n", ans);
}

Greedy:Protecting the Flowers(POJ 3262)的更多相关文章
- 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 贪心(性价比)
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7812 Accepted: ...
- 【POJ - 3262】Protecting the Flowers(贪心)
Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...
- poj 3262 Protecting the Flowers 贪心 牛吃花
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11402 Accepted ...
- BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 448 So ...
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )
考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
- 1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 493 So ...
- bzoj1634 / P2878 [USACO07JAN]保护花朵Protecting the Flowers
P2878 [USACO07JAN]保护花朵Protecting the Flowers 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
随机推荐
- 【CodeForces 606A】A -特别水的题1-Magic Spheres
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/A Description Carl is a beginne ...
- 34.Android之资源文件res里drawable学习
我们经常看到android工程资源文件res下drawable如ldpi.mdpi.hdpi.xhdpi.xxhdpi文件,今天我们学习了解下. (1)drawable-hdpi里面存放高分辨率的图片 ...
- 【BZOJ-2588】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 3749 Solved: 873[ ...
- BZOJ-1968 COMMON 约数研究 数论+奇怪的姿势
1968: [Ahoi2005]COMMON 约数研究 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1513 Solved: 1154 [Submit] ...
- C++ STL初学笔记
C++ STL初学笔记 更系统的版本见徐本柱的PPT set 在这儿:http://www.cnblogs.com/pdev/p/4035020.html #include <vector&g ...
- POJ2594 Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8193 Accepted: 3358 Description Have ...
- jQuery键盘事件绑定Enter键
<script> $(function(){ $(document).keydown(function(event){ if(event.keyCode==13){ $("#mo ...
- Emgu学习之(一)——Emgu介绍
OpenCV“OpenCV是一个开源的计算机视觉库.OpenCV采用C/C++语言编写,可以运行在Linux/Windows/Mac等操作系统上.OpenCV还提供了Python.Ruby.MATLA ...
- ThinkPHP 购物商城网站(数据库中增删改查的功能实现)——————重点——————
控制器 ---------------------GoodsController.class.php------------------------------------------------- ...
- html5视频播放器
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...