【题意】

约翰留下他的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护花的更多相关文章

  1. 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和最后一次出现的次数 ...

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

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

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

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

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

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

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

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

  6. [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

    Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1634 贪心.. 我们发现,两个相邻的牛(a和b)哪个先走对其它的牛无影响,但是可以通过 a的破坏花× ...

  8. BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634 题意: 约翰留下他的N只奶牛上山采木.可是,当他回来的时候,他看到了一幕惨剧:牛们正 ...

  9. 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 ...

随机推荐

  1. sed 之 N D P

    sed的N,D,P 是用于多行模式空间的命令,分别对应于n,d,p n & N: n(next)输出模式空间的内容,然后读取新的输入行,n命令不创建多行模式空间:N(Next)通过读取新的输入 ...

  2. JSP-Runoob:Cookie 处理

    ylbtech-JSP-Runoob:Cookie 处理 1.返回顶部 1. JSP Cookie 处理 Cookie是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JS ...

  3. MySQL 1045登录失败(转)

    http://blog.csdn.net/bbirdsky/article/details/8134528# 当你登录MySQL数据库出现:Error 1045错误时(如下图),就表明你输入的用户名或 ...

  4. Hotel booking(spfa+floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=2992 题意:有n个城市,编号为(1~n),有一些城市中有一些旅店,要求从一个城市到另一个城市不能超过10小时,问 ...

  5. Appium + python - TouchAction操作

    from appium import webdriverfrom appium.webdriver.common.touch_action import TouchActionfrom appium. ...

  6. 绝对牛x的代码注释

    备注:文中字符均可以直接复制直接用! 再补上一个好玩的网站 Ascii World:(链接:http://www.asciiworld.com/). 网站上的图形很多,感兴趣的可以复制链接到浏览器上打 ...

  7. curl 采集的时候遇到301怎么办

    采集的时候遇到301,采集数据有错误 $ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt( ...

  8. Set-----集合入门

    函数中的集合和  数学中的集合 基本上差不多 集合中每个元素最多只能出现一次  并且 当元素储存到set集合之中 会自动 按照 ascll 进行  从小到大的  排序 大神关于   set   的 详 ...

  9. 最大正方形 同luogu1387

    这道题下面这么写就够了(n<=100)暴力,枚举 #include<bits/stdc++.h> #define ULL unsigned long long #define MAX ...

  10. 8.Layers Editor

    图层编辑 Ventuz5中有两种类型的场景,分别是2D图层和3D图层.3D图层包含Content和Hierarchy,而2D图层只包含Content.默认情况下,图层编辑器显示在Ventuz中的左上角 ...