题目大意:

N (1 ≤ N ≤ 50,000)头牛叠罗汉 找到最优排序使所有牛的风险值总和最小

每头牛重量 (1 ≤ Wi ≤ 10,000) 力量 (1 ≤ Si ≤ 1,000,000,000)

自上往下编号1~n,第i头牛的风险值 = i~n的体重总和 - i的力量.

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, Wi and Si.

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.

观察规律:

假设一开始为最优排序

W = W(1) + W(2) + ... + W(i-1) 则

牛(i)风险 = W - S(i)

牛(i+1)风险 = W + W(i)- S(i+1)

若交换位置

牛(i+1)风险 = W - S(i+1)

牛(i)风险 = W + W(i+1)- S(i)

因为一开始为最优排序

所以 W + W(i)- S(i+1)< W + W(i+1)- S(i)

则存在 W(i)+  S(i)< S(i+1)+ W(i+1)

  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. using namespace std;
  4. int n,flag[];
  5. long long m;
  6. struct dlh
  7. {
  8. long long w,s,sum;
  9. }d[];
  10. bool cmp(struct dlh q,struct dlh p)
  11. {
  12. return q.sum>p.sum;
  13. }
  14. int main()
  15. {
  16. scanf("%d",&n);
  17. long long cnt=,ans=-INF;
  18. for(int i=;i<=n;i++)
  19. {
  20. scanf("%d%lld",&d[i].w,&d[i].s);
  21. cnt+=d[i].w;
  22. d[i].sum=d[i].w+d[i].s;
  23. }
  24. sort(d+,d++n,cmp);
  25. for(int i=;i<=n;i++)
  26. {
  27. cnt-=d[i].w;
  28. ans=max(ans,cnt-d[i].s);
  29. }
  30. printf("%d\n",ans);
  31.  
  32. return ;
  33. }

USACO2005 Cow Acrobats /// 前缀和 oj23402的更多相关文章

  1. BZOJ1629: [Usaco2007 Demo]Cow Acrobats

    1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 601  Solved: 305[Su ...

  2. POJ 3045 Cow Acrobats (贪心)

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

  3. Cow Acrobats(贪心)

    Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3686   Accepted: 1428 Desc ...

  4. POJ3045 Cow Acrobats 2017-05-11 18:06 31人阅读 评论(0) 收藏

    Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4998   Accepted: 1892 Desc ...

  5. POJ3045 Cow Acrobats —— 思维证明

    题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  6. poj3045 Cow Acrobats (思维,贪心)

    题目: poj3045 Cow Acrobats 解析: 贪心题,类似于国王游戏 考虑两个相邻的牛\(i\),\(j\) 设他们上面的牛的重量一共为\(sum\) 把\(i\)放在上面,危险值分别为\ ...

  7. 【POJ - 3045】Cow Acrobats (贪心)

    Cow Acrobats Descriptions 农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 & ...

  8. BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1332 题意: 有n头牛在“叠罗汉”. 第i头牛的体重为w[i],力量为s[i]. 一头 ...

  9. [USACO2005][POJ3045]Cow Acrobats(贪心)

    题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风 ...

随机推荐

  1. 其它课程中的python---5、Pandas处理数据和读取数据

    其它课程中的python---5.Pandas处理数据和读取数据 一.总结 一句话总结: 记常用和特例:慢慢慢慢的就熟了,不用太着急,慢慢来 库的使用都很简单:就是库的常用函数就这几个,后面用的时候学 ...

  2. 如何获取监听iframe src属性的变化进行后续操作

    应用场景,当iframe内发生点击事件内容改变时,如果我们想获取变化后的iframe的 src 属性值,就可以使用如下方式去获取 <iframe id="taobaoOrder&quo ...

  3. STM32库中自定义的数据类型

    在头文件 <stdint.h> 中 1 /* exact-width signed integer types */ typedef signed char int8_t; typedef ...

  4. linux centos6安装postgresql

    参考:https://blog.csdn.net/zhu_xun/article/details/21234663 参考:https://www.cnblogs.com/jimcsharp/p/857 ...

  5. flask 的orm

    https://www.cnblogs.com/chichung/p/9794702.html

  6. 020_JUC

    JUC Java.util.Concurrent 并发包 池的顶级接口 Executor 子接口 ExecutorService 工具类 Executors(Collections.Arrays .. ...

  7. ImsConference.java中会议成员更新处理详解

    public class ConferenceParticipant implements Parcelable { //自定义数据结构 private static final String ANO ...

  8. JDK在Win10与Ubuntu下的安装与配置

        本文首发于cartoon的博客     转载请注明出处:https://cartoonyu.github.io/cartoon-blog     近段时间把自己电脑(win).虚拟机(Ubun ...

  9. CKEditor与CKFinder学习--自定义界面及按钮事件捕获

    原文地址:CKEditor与CKFinder学习--自定义界面及按钮事件捕获  讨厌CSDN的广告,吃香太难看! 效果图 界面操作图 原始界面 调整后的界面(删除了flush,表单元素等) 该界面的皮 ...

  10. tftp 服务器的配置

    如果用下面一条命令能够看到服务已经启动, 则不用安装, 否则需要按 1 或 2 点安装 tftp-server 服务器. [arm@localhost arm]#netstat -a | grep t ...