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 ...
随机推荐
- poj 1442 名次树
这回要求的是第k小的元素, 参考了ljl大神的模板,orz //insert 插入 //remove 删除 //_find 查找 //kth 返回root为根的树中第k小的元素 //treap插入.删 ...
- JAVA-封装
1.什么是封装? 顾名思义,封装就是装起来,圈起来的意思,用于类与对象中来讲,就是在一个类中把对象拥有的属性和隐藏信息(条件)进行封装,不允许外部程序直接访问,而必须要通过该类提供的方法来实现对隐藏信 ...
- poj1733Parity game
Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7288 Accepted: 2833 Descr ...
- kaili linux中文乱码
install kaili 用了kali,安装的英文版,一切都很爽,没有乱码 install chrome 不要用apt安装,也不要用chromium, 直接去网上找chrom的deb文件下来安装,注 ...
- CSS 仿Excel表格功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mlecms v2.2版权
inc\tools\smarty 下的Smarty.class.php文件. 找到 187行左右 我们会发现原来的 $dopud =$_template->libfile($dopud);已经 ...
- Spark Graphx In Action
两个重要的技术:Spark和graphs 本章节内容 为什么Spark是最先进的大数据处理系统 是什么让图可以以一种独特的方式来模拟关联数据 GraphX为什么会成规领先的图分析平台
- mappedBy reference an unknown target entity property解决方法
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error ...
- JNI环境变量——JNIEnv*的使用 &&配置jd环境变量
如果没有配置环境变量,先配置环境变量,如下: 1.右键我的电脑——高级——环境变量——下面的系统变量 2.选择[新建系统变量]--弹出“新建系统变量”对话框,在“变量名”文本框输入“JAVA_HO ...
- AndroidStudio-引用jar包及so文件
一.引用jar文件 1.将jar文件复制.粘贴到app的libs目录中: 2.右键点击jar文件,并点击弹出菜单中的"Add As Library",将jar文件作为类 ...