1629: [Usaco2007 Demo]Cow Acrobats

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 601  Solved: 305
[Submit][Status]

Description

Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing acrobatic stunts. The cows aren't terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves within this stack. Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows. //有三个头牛,下面三行二个数分别代表其体重及力量 //它们玩叠罗汉的游戏,每个牛的危险值等于它上面的牛的体重总和减去它的力量值,因为它要扛起上面所有的牛嘛. //求所有方案中危险值最大的最小

Input

* Line 1: A single line with the integer N. * Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.

Output

* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.

Sample Input

3
10 3
2 5
3 3

Sample Output

2

OUTPUT DETAILS:

Put the cow with weight 10 on the bottom. She will carry the other
two cows, so the risk of her collapsing is 2+3-3=2. The other cows
have lower risk of collapsing.

HINT

 

Source

题解:
同国王游戏,只不是按a[i]+b[i]递增排序。。。
正确性可以从 任意交换两个a[i]+b[i]逆序的,答案不会变差来考虑
WA了一次是因为危险值竟然还有负的。。。
代码:
 
  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<iostream>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<queue>
  11. #include<string>
  12. #define inf 1000000000
  13. #define maxn 50000+100
  14. #define maxm 500+100
  15. #define eps 1e-10
  16. #define ll long long
  17. #define pa pair<int,int>
  18. using namespace std;
  19. inline int read()
  20. {
  21. int x=,f=;char ch=getchar();
  22. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  23. while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
  24. return x*f;
  25. }
  26. int n;
  27. struct rec{int x,y;}a[maxn];
  28. inline bool cmp(rec a,rec b)
  29. {
  30. return a.x+a.y<b.x+b.y;
  31. }
  32. int main()
  33. {
  34. freopen("input.txt","r",stdin);
  35. freopen("output.txt","w",stdout);
  36. n=read();
  37. for(int i=;i<=n;i++)a[i].x=read(),a[i].y=read();
  38. sort(a+,a+n+,cmp);
  39. int sum=,ans=-inf;
  40. for(int i=;i<=n;i++)
  41. {
  42. ans=max(ans,sum-a[i].y);
  43. sum+=a[i].x;
  44. }
  45. printf("%d\n",ans);
  46. return ;
  47. }

BZOJ1629: [Usaco2007 Demo]Cow Acrobats的更多相关文章

  1. BZOJ 1629: [Usaco2007 Demo]Cow Acrobats

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  2. 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...

  3. [USACO2007 Demo] Cow Acrobats

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1629 [算法] 贪心 考虑两头相邻的牛 , 它们的高度值和力量值分别为ax , ay ...

  4. bzoj 1629: [Usaco2007 Demo]Cow Acrobats【贪心+排序】

    仿佛学到了贪心的新姿势-- 考虑相邻两头牛,交换它们对其他牛不产生影响,所以如果交换这两头牛能使这两头牛之间的最大值变小,则交换 #include<iostream> #include&l ...

  5. 【刷水-贪心】BZOJ1629-[Usaco2007 Demo]Cow Acrobats

    [题目大意] 有n个头牛,给出体重和力量.每个牛的危险值等于它上面的牛的体重总和减去它的力量值,求所有方案中危险值最大值的最小值. [思路] 贪心.一开始脑补的贪心是体重大的先放下面,体重相同的根据力 ...

  6. BZOJ1631: [Usaco2007 Feb]Cow Party

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 459  Solved: 338[Submit ...

  7. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

  8. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

  9. BZOJ1638: [Usaco2007 Mar]Cow Traffic 奶牛交通

    1638: [Usaco2007 Mar]Cow Traffic 奶牛交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 571  Solved: 199 ...

随机推荐

  1. arm-linux移植MT7601Uusb无线网卡(小度wifi,360随身WIFI 2代)

    前段时间移植过RT3070.RT5370 http://blog.csdn.net/ofaith12345/article/details/24138399 发现各种arm移植都大同小异,所以就不要纠 ...

  2. Android(java)学习笔记226:服务(service)之为什么使用服务

    1.服务 service 长期在后台运行的进程,一般没有应用程序界面   2.进程线程和应用程序之间的关系 应用程序开启,系统启动一个Linux进程,所有的组件都是运行在同一个进程的同一个线程(mai ...

  3. php-fpm 启动参数及重要配置详解<转>

    原文地址  http://levi.cg.am/archives/3127 约定几个目录 /usr/local/php/sbin/php-fpm /usr/local/php/etc/php-fpm. ...

  4. 9.13noip模拟试题

    题目名称 “与” 小象涂色 行动!行动! 输入文件 and.in elephant.in move.in 输出文件 and.out elephant.in move.in 时间限制 1s 1s 1s ...

  5. codevs 1515 跳

    /* 画矩阵找规律发现是杨辉三角 Cg (i,j)= C (i+j,i); 贪心走的话 沿着0行(列)一直走然后拐直角 拐弯后每个格子都累加 Cg (n,0) + Cg (n,1) + Cg (n,2 ...

  6. 使用phpmailer发送邮件(以QQ邮箱为例)

    <?php   include("class/class.phpmailer.php"); //下载phpmailer并include两个文件  include(" ...

  7. windows服务安装(System.ComponentModel.Win32Exception:远程过程调用失败)

    “安装”阶段发生异常.System.ComponentModel.Win32Exception:远程过程调用失败 附上提示信息C:\Windows\Microsoft.NET\Framework\v4 ...

  8. MySQL中TIMESTAMP和DATETIME区别

    1.两者的存储方式不一样 TIMESTAMP:把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储.查询时,将其又转化为客户端当前时区进行返回. DATETIME:不做任何改变,基本上是原 ...

  9. MVC跳转

    //RedirectToAction(view?参数,控制器); return RedirectToAction("MyjoinEvent?id=" + eventid + &qu ...

  10. $(document).ready(function(){}),$().ready(function(){})和$(function(){})三个有区别么

    三者都是一样的,最完整的写法是:$(document).ready(function(){})ready() 函数仅能用于当前文档,因此无需选择器.所以document选择器可以不要,那么就可以写成: ...