BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花
【题意】
约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小,约翰赶紧采取行动,把牛们送回牛棚. 牛们从1到N编号.第i只牛所在的位置距离牛棚Ti(1≤Ti≤2000000)分钟的路程,而在约翰开始送她回牛棚之前,她每分钟会啃食Di(1≤Di≤100)朵鲜花.无论多么努力,约翰一次只能送一只牛回棚.而运送第第i只牛事实上需要2Ti分钟,因为来回都需要时间. 写一个程序来决定约翰运送奶牛的顺序,使最终被吞食的花朵数量最小.
【题解】
偏序问题,可以先计算两个的情况,写出Cmp函数,再推广到一般情况。
我们可以发现对于相邻的两头牛a与b,若满足a.t*b.d<b.t*a.d,则a放在b前面更优。因为a放在后面的话会吃掉更多的花。
那么若要使被吃的花数量最少,即要使序列满足Ai.t*Ai+1.d<Ai+1.t*Ai.d。
直接对序列排序即可。
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=;
LL n,ans,now;
struct rec{LL t,d;}a[maxn];
void read(LL &k){
k=; int f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
k*=f;
}
bool cmp(rec a,rec b){return a.d*b.t>b.d*a.t;}
int main(){
read(n);
for (int i=;i<=n;i++) read(a[i].t),read(a[i].d);
sort(a+,a+n+,cmp);
for (int i=;i<=n;i++) ans+=a[i].d*now,now+=a[i].t*;
return printf("%lld\n",ans),;
}
BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花的更多相关文章
- BZOJ 4511 洛谷3131 USACO 16.Jan 七子共
用sum[i]表示前缀和模7的值,若存在i≤j,满足sum[i]==sum[j],则区间(i,j]的和为7的倍数. O(N)扫出sum[0]~sum[6]第一次出现的位置first和最后一次出现的次数 ...
- 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: [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 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
- 【BZOJ】1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1634 贪心.. 我们发现,两个相邻的牛(a和b)哪个先走对其它的牛无影响,但是可以通过 a的破坏花× ...
- BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634 题意: 约翰留下他的N只奶牛上山采木.可是,当他回来的时候,他看到了一幕惨剧:牛们正 ...
- 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 ...
随机推荐
- MapReduce01
================== Hadoop内核 | MapReduce(分布式计算框架) ================== 源于Google的MapReduce论文 ----------& ...
- java dom4j 读写XML
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Config id ...
- bzoj2194
http://www.lydsy.com/JudgeOnline/problem.php?id=2194 卷积... 卷积并不高深,其实卷积就是两个多项式相乘的系数,但是得满足一点条件,就是f[n]= ...
- SmartDispatcher 类
UI线程中使用 public class SmartDispatcher { public static void BeginInvoke(Action action) { if (Deploymen ...
- ubuntu下设置共享目录
在使用VirtualBox和相关的客户机系统比如XPMac等需要用到一些相关功能共享剪贴板等等这时候需要安装VirtualBox中的一个工具叫做Guest Additions中文叫法不一增强工具包功能 ...
- codeforces——模拟
805 B. 3-palindrome http://codeforces.com/problemset/problem/805/B /* 题意字符串中不能有长度为三的回文串,且c数量最少 */ ...
- [Swift通天遁地]三、手势与图表-(9)制作五彩缤纷的气泡图表
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Android内存管理(9)*MAT:Heap Dump,Shallow Heap,Retained Heap,Dominating Tree,GC Roots等的含义
原文: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fconcepts%2Fheapdump.ht ...
- 【Java基础】多态
首先先来个总结: 什么是多态 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. 多态的定义:指允许不同类的对象对同一消 ...
- [ ZJOI 2006 ] Mahjong
\(\\\) \(Description\) 现有权值分别为\(1\text~100\)的\(100\)种牌,分别给出每种排的张数\(A_i\),试判断能否胡牌,胡牌需要将所有牌不重不漏地分成以下几类 ...