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 ...
随机推荐
- 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1685 Solved: 724[Submit] ...
- ACdream 1429 Rectangular Polygon
Rectangular Polygon Time Limit: 1000MS Memory Limit: 256000KB 64bit IO Format: %lld & %llu D ...
- hihocoder #1034 毁灭者问题
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在 Warcraft III 之冰封王座中,毁灭者是不死族打三本后期时的一个魔法飞行单位. 毁灭者的核心技能之一, ...
- py替换掉换行符
for line in file.readlines(): line=line.strip('\n')
- -----------------------------------项目中整理的非常有用的PHP函数库(一)-----------------------------------------------------
1.PHP加密解密 PHP加密和解密函数可以用来加密一些有用的字符串存放在数据库里,并且通过可逆解密字符串,该函数使用了base64和MD5加密和解密. function encryptDecrypt ...
- --hdu 1050 Moving Tables(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 AC code: #include<stdio.h> #include<str ...
- udf提权方法和出现问题汇总
一.适用条件 1.目标系统是Windows(Win2000,XP,Win2003): 2.你已经拥有MYSQL的某个用户账号,此账号必须有对mysql的insert和delete权限以创建和抛弃函数( ...
- WPF捕捉Windows关机事件
private const int SC_SCREENSAVE = 0xF140; private const int WM_QUERYENDSESSION = 0x0011; private boo ...
- javax.inject中@Inject、@Named、@Qualifier和@Provider用法
@Inject @Inject支持构造函数.方法和字段注解,也可能使用于静态实例成员.可注解成员可以是任意修饰符(private,package-private,protected,public).注 ...
- linux 中\r
工作中遇到的一个处理\r和\n的问题,看了一下\r是啥... void File_translater::EspEntr(string& strSrc){ int iPos; while((i ...