保护花朵

  题目大意:就是农夫有很多头牛在践踏花朵,这些牛每分钟破坏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)的更多相关文章

  1. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  2. POJ 3262 Protecting the Flowers 贪心(性价比)

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7812   Accepted: ...

  3. 【POJ - 3262】Protecting the Flowers(贪心)

    Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...

  4. poj 3262 Protecting the Flowers 贪心 牛吃花

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11402   Accepted ...

  5. BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 448  So ...

  6. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )

    考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...

  7. 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  So ...

  8. bzoj1634 / P2878 [USACO07JAN]保护花朵Protecting the Flowers

    P2878 [USACO07JAN]保护花朵Protecting the Flowers 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...

  9. [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 885  So ...

随机推荐

  1. Rootkit Hacking Technology && Defence Strategy Research

    目录 . The Purpose Of Rootkit . Syscall Hijack . LKM Module Hidden . Network Communication Hidden . Fi ...

  2. CentOS 6.3 NFS的安装配置、启动及mount挂载方法

    一.环境介绍: 服务器:centos 192.168.1.225 客户端:centos 192.168.1.226 二.安装: NFS的安装配置: centos 5 : yum -y install ...

  3. 初学Hibernate

    Hibernate 是完全ORM的,只需要对 对象 进行操作,生成底层SQL语句 优势:1.可以简化开发 2.性能好(原生的Hibernate性能很差,要使用它,需要进行优化),优化方式:一级缓存.二 ...

  4. 移动端bug总结

    1. 移动端按钮点击蓝框去除 button,input[type="button"] { outline:none; }

  5. nginx 服务器重启命令,关闭 (转)

    nginx -s reload  :修改配置后重新加载生效nginx -s reopen  :重新打开日志文件nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否 ...

  6. Java-TCP Socket编程

    TCP 的 Java 支持 协议相当于相互通信的程序间达成的一种约定,它规定了分组报文的结构.交换方式.包含的意义以及怎样对报文所包含的信息进行解析,TCP/IP 协议族有 IP 协议.TCP 协议和 ...

  7. Linux CP文件夹略过目录的解决

    在Ubuntu Linux复制文件夹时出现一个问题,做个笔记,希望能给刚入门的菜鸟学习一下(见图1). 出现略过目录: www_linuxidc_com@linuxidc-Aspire-3680:~$ ...

  8. JavaScript简易缩放程序

    一.前言: 上一篇随笔中已经把拖动程序完成了,这篇主要把缩放程序完成,后面合并后可以做成一个图片裁剪的功能 简易缩放程序DEMO:http://jsfiddle.net/UN99R/ 限制缩放程序DE ...

  9. 淘宝(阿里百川)手机客户端开发日记第八篇 Handler的使用方法

    首先,我们先看下API文档的说明: A Handler allows you to send and process Message and Runnable objects associated w ...

  10. http://backboneconf.com/ @前端 真好

    http://backboneconf.com/ @前端http://backboneconf.com/ @前端http://backboneconf.com/ @前端http://backbonec ...