题目大意:

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)

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int n,flag[];
long long m;
struct dlh
{
long long w,s,sum;
}d[];
bool cmp(struct dlh q,struct dlh p)
{
return q.sum>p.sum;
}
int main()
{
scanf("%d",&n);
long long cnt=,ans=-INF;
for(int i=;i<=n;i++)
{
scanf("%d%lld",&d[i].w,&d[i].s);
cnt+=d[i].w;
d[i].sum=d[i].w+d[i].s;
}
sort(d+,d++n,cmp);
for(int i=;i<=n;i++)
{
cnt-=d[i].w;
ans=max(ans,cnt-d[i].s);
}
printf("%d\n",ans); return ;
}

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. JS 变量 相关内容

    JS变量按存储方式区分为哪些类型?: js变量按照存储方式分为两种类型:值类型 和 引用类型 1.值类型(基本类型): 布尔值(boolean) . null .undefined .数值(numbe ...

  2. share memory

    header for public argument:shmdata.h #define TEXT_SZ 2048 struct shared_use_st { int written; char t ...

  3. Future初次使用理解

    当客户端执行方法时,立即返回一个代理对象,此时代理对象没有数据,与此同时开启一个线程去构造真实对象并把真实对象替换掉代理对象(使用set方法).所以就会出现,客户端收到代理对象之后以为执行完了然后执行 ...

  4. 2019杭电多校第三场hdu6608 Fansblog(威尔逊定理)

    Fansblog 题目传送门 解题思路 Q! % P = (P-1)!/(P-1)...(Q-1) % P. 因为P是质数,根据威尔逊定理,(P-1)!%P=P-1.所以答案就是(P-1)((P-1) ...

  5. POJ 1269 Intersecting Lines (判断直线位置关系)

    题目链接:POJ 1269 Problem Description We all know that a pair of distinct points on a plane defines a li ...

  6. ActionContext 与 ServletActionContext获取Session的异同

    1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息, ...

  7. JDK8新特性之Stream流

    是什么是Stream流 java.util.stream.Stream Stream流和传统的IO流,它们都叫流,却是两个完全不一样的概念和东西. 流可以简单的说是处理数据集合的东西,可以申明式流式A ...

  8. echart 柱状图背景色设置

    方法一: series: [ { name: '1', type: 'bar', itemStyle: {//柱图背景色 color: '#111' }, data: list }, { name: ...

  9. 2018-2-13-win10-uwp-让焦点在点击在页面空白处时回到textbox中

    title author date CreateTime categories win10 uwp 让焦点在点击在页面空白处时回到textbox中 lindexi 2018-2-13 17:23:3 ...

  10. 深度探索C++对象模型读书笔记-第六章执行期语意学

    在函数中,编译器会帮助将析构函数(Destructor) 安插在相应的位置.对于函数中的局部对象,会将析构函数安插在对象的每一个离开点. 例如: 1: void Function(int a) { 2 ...