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 ithin 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

Hint

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.

题目大意

求出每一个元素右边第一个比它大的元素(仰望对象)

思路分析

朴素算法就是遍历一遍整个序列,对于每个元素,依次往右扫,扫到第一个比他大的元素,就记录下来。这样的时间复杂度为O(n^2),肯定要超时的。我们发现每次往右扫有重复的过程,那么能不能把每次扫描的过程记录下来呢?
首先肯定扫的话是从右往左扫,最右边的元素没有仰望对象,记成0,倒数第二个元素仰望对象肯定是最后边的元素,依次往左遍历,如果新加入的数字比之前的数字还要大的话,要之前的数字也就没有用了(对于这个数来说,我要找比它大的数,比它小的数没用,对于这个数左边的数,这个数都已经比那些数要大,跟他们就更没关系了),就让他们出栈,而出栈后栈顶元素就是我们要找的栈顶元素。

[单调栈]Cow Acrobats相关推荐

  1. 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)

    题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self ...

  2. 【POJ - 3250 】Bad Hair Day (单调栈)

    题干: Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self- ...

  3. POJ3250(单调栈)

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17614   Accepted: 5937 Des ...

  4. bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈)

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 848  Solved: 594 [Sub ...

  5. [USACO09MAR]向右看齐Look Up(单调栈、在线处理)

    https://www.luogu.org/problem/P2947 题目描述 Farmer John's N (1 <= N <= 100,000) cows, convenientl ...

  6. 洛谷 - [USACO09MAR]向右看齐Look Up(单调栈)

    题目链接:https://www.luogu.org/problem/P2947 时间限制: 1.00s 内存限制: 125.00MB 题目描述 Farmer John's N ( 1 ≤ N ≤ 1 ...

  7. 蒟蒻的ACM数据结构(四)-单调队列和单调栈

    单调队列和单调栈 一.概念 二.实现 三.题目 单调队列 洛谷P1886 滑动窗口 解析 单调栈 [GXOI/GZOI2019]与或和 解析 POJ3250 Bad Hair Day 解析 POJ 2 ...

  8. POJ2796 Feel Good(单调栈)

    题意: 给出一列数据,要求一个区间内最小值与区间内数据总和乘积最大值 要点: 还是单调栈,这次我自己写的,先做了几题比较简单的果然还是有效果的,这题也是一样,按点遍历,网上大神做的是直接遍历一次即可, ...

  9. 【单调栈 前缀和 异或】7.21序列求和

    还要再细细思考的奇妙思路 题目描述 小A最近喜欢上了关于区间max的问题.她定义一个区间的价值是max(ai)(l<=i<=r)∗(alxoral+1xor...xorar)max(ai) ...

最新文章

  1. SparkSQL核心编程
  2. openwrt路由器打印机服务器设置_DB120搭建hp1018 OpenWrt打印服务器
  3. sbt構建一個spark工程(scala+spark+sbt)
  4. 思科asa5515端口映射_Cisco ASA端口映射
  5. asp listview 单元格文字颜色_如何替换CAD表格中特定区域单元格中的文字?
  6. 【转】ABP源码分析二十八:ABP.MemoryDB
  7. 辽宁省计算机专业A类,辽宁省中职升高职招生考试计算机及应用专业综合课试卷(共2份)...
  8. Android中视图重绘的方法
  9. python自带的库有哪些餐厅_Python 常用的标准库以及第三方库有哪些
  10. java生成16位唯一性的订单号
  11. jmeter录制 过滤_jmeter脚本录制方法:
  12. 安卓工具类集合—— 1 时间、时间戳转换工具
  13. 关于微信小程序自定义交易组件升级处理的相关问题,及解决思路
  14. Android中EditText隐藏/自动弹出输入法的问题
  15. 初闻“Linux中的cp,mv,rm”
  16. 西普学院Crypto之先有鸡还是先有蛋
  17. java开发本科应届生薪资大概是多少?外行人最少15k,25k是人均水平...
  18. cad序列号2016申请号_安装 CAD 踩坑
  19. Windows Server2016 WSUS服务部署
  20. 高通打开wifi驱动的log

热门文章

  1. 关于jq工具安装shell脚本
  2. RKE安装Kubernetes
  3. Maven不能下载Aspose解决方案
  4. 计算机网络思科答案参考--以太网的概念考试
  5. 美云智数 为您提供优质的数据解决方案
  6. “坐不住”的融创集团,压力已至
  7. 计算机房冷热概算指标,空调水系统设计经验分享,机房+冷负荷计算+冷冻水,彻底搞懂它...
  8. CPU性能过剩提升乏力影响未来行业发展吗?
  9. 疫情危机下,欧洲风投青睐的初创趋势
  10. 电子科技大学计算机保研经历,电子科技大学计算机科学与工程学院(专业学位)计算机技术保研...