BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634
题意:
约翰留下他的N只奶牛上山采木。可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!
为了使接下来花朵的损失最小,约翰赶紧采取行动,把牛们送回牛棚。
第i只牛所在的位置距离牛棚t[i](1 <= t[i] <= 2000000)分钟的路程,而在约翰开始送她回牛棚之前,她每分钟会啃食e[i](1 <= e[i] <= 100)朵鲜花。
无论多么努力,约翰一次只能送一只牛回棚。而运送第第i只牛事实上需要2Ti分钟,因为来回都需要时间。
写一个程序来决定约翰运送奶牛的顺序,使最终被吞食的花朵数量最小。
题解:
贪心。
对于顺序相邻的两只牛a和b,交换a和b的顺序,对于a和b之外的牛是没有影响的。
将其他的牛看作一只牛c。
当a排在b之前时,答案为:
ans1 = t[a]*(e[b]+e[c]) + t[b]*e[c]
当b排在a之前时,答案为:
ans2 = t[b]*(e[a]+e[c]) + t[a]*e[c]
假设a排在b前面的时候答案更优,则有:
ans1 < ans2
即:t[a]*(e[b]+e[c]) + t[b]*e[c] < t[b]*(e[a]+e[c]) + t[a]*e[c]
整理得:t[a]*e[b] < t[b]*e[a]
所以按照t[a]*e[b] < t[b]*e[a]排序就好了。
AC Code:
// before: t[a]*(e[b]+e[c]) + t[b]*e[c]
// after: t[b]*(e[a]+e[c]) + t[a]*e[c]
// if a is better:
// t[a]*(e[b]+e[c]) + t[b]*e[c] < t[b]*(e[a]+e[c]) + t[a]*e[c]
// t[a]*e[b] + t[a]*e[c] + t[b]*e[c] < t[b]*e[a] + t[b]*e[c] + t[a]*e[c]
// t[a]*e[b] < t[b]*e[a]
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAX_N 100005 using namespace std; struct Cow
{
int eat;
int tim;
Cow(int _eat,int _tim)
{
eat=_eat;
tim=_tim;
}
Cow(){}
friend bool operator < (const Cow &a,const Cow &b)
{
return a.tim*b.eat<b.tim*a.eat;
}
}; int n;
long long ans=;
Cow cow[MAX_N]; void read()
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>cow[i].tim>>cow[i].eat;
cow[i].tim<<=;
}
} void solve()
{
sort(cow,cow+n);
int tot=;
for(int i=;i<n;i++)
{
tot+=cow[i].eat;
}
for(int i=;i<n;i++)
{
tot-=cow[i].eat;
ans+=cow[i].tim*tot;
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】的更多相关文章
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )
考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花
Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the g ...
- bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花【贪心】
因为交换相邻两头牛对其他牛没有影响,所以可以通过交换相邻两头来使答案变小.按照a.t*b.f排降序,模拟着计算答案 #include<iostream> #include<cstdi ...
- 1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 493 So ...
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
- 【BZOJ】1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1634 贪心.. 我们发现,两个相邻的牛(a和b)哪个先走对其它的牛无影响,但是可以通过 a的破坏花× ...
- 【bzoj1634】[Usaco2007 Jan]Protecting the Flowers 护花 贪心
题目描述 Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, a ...
- BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 448 So ...
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
随机推荐
- Python 可视化Twitter中指定话题中Tweet的词汇频率
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-8 @author: guaguastd @name: pl ...
- 0x…… is not a valid instance ID怎么解决
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Spring中的scope配置和@scope注解
Scope,也称作用域,在 Spring IoC 容器是指其创建的 Bean 对象相对于其他 Bean 对象的请求可见范围.在 Spring IoC 容器中具有以下几种作用域:基本作用域(single ...
- C语言 包含结构的结构
一个结构体的成员是另一个结构体 代码: # include <stdio.h> # include <stdlib.h> struct data { int year; int ...
- Qt QImageReader 相似乎有bug
Qt 版本号 5.4.1 (VS2010) 近期在做一个小程序,事实上非常easy,就是打开一个gif 动画.能够静态显示当中随意一帧图像.Qt 中有一个QImageReader 类.用这个类理论上说 ...
- VxWorks启动过程具体解释(下)
上一节主要是从映像的分类和各种映像的大致载入流程上看VxWorks的启动过程,这一节让我们从函数级看一下VxWorks的启动过程: 1. Boot Image + Loadable Images: 以 ...
- Spark源代码分析之六:Task调度(二)
话说在<Spark源代码分析之五:Task调度(一)>一文中,我们对Task调度分析到了DriverEndpoint的makeOffers()方法.这种方法针对接收到的ReviveOffe ...
- jQuery中slideToggle()的详细使用方法和解释
$(selector).slideToggle(speed,callback) 参数 speed和callback Speed 可选.规定元素从隐藏到显示的速度,默认‘normal’可能 ...
- python 基础 2.2 if流程控制(二)
一. if else 1.逻辑值(bool)包含了两个值: ----True:表示非空的值,比如:string ,tuple,list,set,dictonary,所有非空的序列. -----F ...
- EasyPusher华为手机直播推流硬编码[OMX.IMG.TOPAZ.Encoder] failed to set input port definition parameters.
EasyPusher作为一款RTSP推送利器, 配合EasyDarwin开源流媒体服务器,在发布伊始,很快获得了广大人民群众的一致好评. 但是也有一些用户反映: EasyPusher在我的华为手机上会 ...