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 ...
随机推荐
- session共享
Nginx或者Squit反向代理到两台tomcat服务器 tomcat使用memcached tomcat连接memcached工具 cp session/*.jar /usr/local/tomca ...
- 16.(转) Android之Support v4、v7、v13的区别和应用场景
我们在项目中经常会碰到Android Support v4.v7和v13包兼容问题,所以有必要梳理下这些东西. google提供了Android Support Library package 系列的 ...
- TYVJ1038 忠诚
hzw学长博客里的2048,根本停不下来! 描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要 求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意. ...
- Robot Framework测试框架学习笔记
一.Robot Framework框架简介 Robot Framework是一种基于Python的可扩展关键字驱动自动化测试框架,通常用于端到端的可接收测试和可接收测试驱动的开发.可以 ...
- jQuery返回顶部(精简版)
jQuery返回顶部(精简版) <!DOCTYPE html><html lang="en"><head> <meta charset=& ...
- 深入浅出Redis04使用Redis数据库(lists类型)
一 lists类型及操作 List是一个链表结构,主要功能是push,pop,获取一个范围的所有值等等,操作中key理解为链表的名字. Redis的list类型其实就是一个每个子元素都是sring类 ...
- Python序列的切片操作与技巧
切片操作 对于具有序列结构的数据来说,切片操作的方法是:consequence[start_index: end_index: step]. start_index: 表示是第一个元素对象,正索引位置 ...
- 不引用office动态库导出excel
public class OutExcelReport { /// <summary> /// 把 DataSet 的数据导成 Excel /// </summary> /// ...
- 深入浅出JSON
JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成.它基于ECMA262语言规范(1999 ...
- Entity Framework 系统约定配置
前言 Code First之所以能够让开发人员以一种更加高效.灵活的方式进行数据操作有一个重要的原因在于它的约定配置.现在软件开发越来越复杂,大家都试图将软件设计的越来越灵活,很多内容我们都希望是可配 ...