题目链接: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:

  1. // before: t[a]*(e[b]+e[c]) + t[b]*e[c]
  2. // after: t[b]*(e[a]+e[c]) + t[a]*e[c]
  3. // if a is better:
  4. // t[a]*(e[b]+e[c]) + t[b]*e[c] < t[b]*(e[a]+e[c]) + t[a]*e[c]
  5. // t[a]*e[b] + t[a]*e[c] + t[b]*e[c] < t[b]*e[a] + t[b]*e[c] + t[a]*e[c]
  6. // t[a]*e[b] < t[b]*e[a]
  7. #include <iostream>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <algorithm>
  11. #define MAX_N 100005
  12.  
  13. using namespace std;
  14.  
  15. struct Cow
  16. {
  17. int eat;
  18. int tim;
  19. Cow(int _eat,int _tim)
  20. {
  21. eat=_eat;
  22. tim=_tim;
  23. }
  24. Cow(){}
  25. friend bool operator < (const Cow &a,const Cow &b)
  26. {
  27. return a.tim*b.eat<b.tim*a.eat;
  28. }
  29. };
  30.  
  31. int n;
  32. long long ans=;
  33. Cow cow[MAX_N];
  34.  
  35. void read()
  36. {
  37. cin>>n;
  38. for(int i=;i<n;i++)
  39. {
  40. cin>>cow[i].tim>>cow[i].eat;
  41. cow[i].tim<<=;
  42. }
  43. }
  44.  
  45. void solve()
  46. {
  47. sort(cow,cow+n);
  48. int tot=;
  49. for(int i=;i<n;i++)
  50. {
  51. tot+=cow[i].eat;
  52. }
  53. for(int i=;i<n;i++)
  54. {
  55. tot-=cow[i].eat;
  56. ans+=cow[i].tim*tot;
  57. }
  58. }
  59.  
  60. void print()
  61. {
  62. cout<<ans<<endl;
  63. }
  64.  
  65. int main()
  66. {
  67. read();
  68. solve();
  69. print();
  70. }

BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】的更多相关文章

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

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

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

  3. bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花【贪心】

    因为交换相邻两头牛对其他牛没有影响,所以可以通过交换相邻两头来使答案变小.按照a.t*b.f排降序,模拟着计算答案 #include<iostream> #include<cstdi ...

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

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  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. 【BZOJ】1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)

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

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

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

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

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

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

随机推荐

  1. OSX:设置用户默认浏览器

    近期我们遇到的情况是,须要统一设置用户的默认浏览器为Google Chrome.而系统默认的是Safari. 这个设置是系统Launch Services基于用户管理的. 意思就是说,即便是改动了系统 ...

  2. 安装centos出错

    在vitural Box中安装centos,出现了如下问题,重新下一遍就好了,如果网速很慢,下载的过程中总是断断续续的就容易出现下载文件损坏的问题. Could not get the storage ...

  3. Swift初窥----语法进阶

    缺省绑定(Optional Binding 自己主动置空) 通过在类型变量后,加上?,能够实现缺省绑定为nil var window: UIWindow? 就是说,假设不正确window赋值,则win ...

  4. 应用程序之SingleViewApplication

    理论概念学习 iOS运行原理 代码结构分析 代码初步实现 一.理论学习 1⃣️.每一个应用程序都有属于自己的UIWindow,继承自UIView 2⃣️.每一个满屏的UIView都由一个UIViewC ...

  5. 检验 java 基础数据类型参数传递方式

    测试证明,java基础数据类型参数传递值虽是引用传递但是值不会改变.对象是引用传递,值会改变. 为什么?找到一段话来解释这个问题. "对于字符串对象来说,虽然在参数传递的时候也是引用传递,但 ...

  6. OpenCV视频读取播放,视频转换为图片

    转载请注明出处!!! http://blog.csdn.net/zhonghuan1992 OpenCV视频读取播放,视频转换为图片 介绍几个有关视频读取的函数: VideoCapture::Vide ...

  7. C# 实现和调用自定义扩展方法

    定义和调用扩展方法 定义一个静态类以包含扩展方法.该类必须对客户端代码可见. 将该扩展方法实现为静态方法,并使其至少具有与包含类相同的可见性. 该方法的第一个参数指定方法所操作的类型:该参数必须以 t ...

  8. SpringBoot开启https以及http重定向

    一.使用JDK keytool创建SSL证书 进入$JAVA_HOME/bin目录,运行以下命令 keytool -genkey -alias WeChatAppletsDemo -keypass - ...

  9. [转]XMPP基本概念--节(stanza)

    本文介绍在XMPP通信中最核心的三个XML节(stanza).这些节(stanza)有自己的作用和目标,通过组织不同的节(stanza),就能达到我们各种各样的通信目的. 首先我们来看一段XMPP流. ...

  10. android 集成QQ互联 (登录,分享)

    参考:http://blog.csdn.net/syz8742874/article/details/39271117 http://blog.csdn.net/woblog/article/deta ...