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了一次是因为危险值竟然还有负的。。。
代码:
 
 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 50000+100
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n;
struct rec{int x,y;}a[maxn];
inline bool cmp(rec a,rec b)
{
return a.x+a.y<b.x+b.y;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for(int i=;i<=n;i++)a[i].x=read(),a[i].y=read();
sort(a+,a+n+,cmp);
int sum=,ans=-inf;
for(int i=;i<=n;i++)
{
ans=max(ans,sum-a[i].y);
sum+=a[i].x;
}
printf("%d\n",ans);
return ;
}

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. PHP自学之路---报表及绘图技术

    Ø  报表 a)        什么是报表 报表就是用表格.图表等格式来动态显示数据. b)        PHP绘图坐标系 1.  坐标系介绍 下图说明了PHP坐标系.坐标原点位于左上角,以像素为单 ...

  2. SEO为什么越来越难?

    不是很长的折腾seo该,无脑想如何很长的头发外链,一方面,没有花费的时间SEO在,另一方面SEO越来越难,的另一个方面,也SEO特征,不可控和不可预测性,致使我们花了非常多的情况下,SEO大部分时间都 ...

  3. visul svn+花生壳

    1.服务器端 工具:visul svn+花生壳 花色壳:注册域名 visul svn:配置http://www.cnblogs.com/bluewelkin/p/3479105.html 外网访问,端 ...

  4. 第一篇:R语言数据可视化概述(基于ggplot2)

    前言 ggplot2是R语言最为强大的作图软件包,强于其自成一派的数据可视化理念.当熟悉了ggplot2的基本套路后,数据可视化工作将变得非常轻松而有条理. 本文主要对ggplot2的可视化理念及开发 ...

  5. Java基础知识强化17:JAVA不可以将所覆盖的方法的访问权限变得比父类的小

    首先我们看一下下面的代码如下: interface I { void go(); } abstract class A implements I { } class C extends A { voi ...

  6. android requestWindowFeature使用详解

    requestWindowFeature可以设置的值有:          1.DEFAULT_FEATURES:系统默认状态,一般不需要指定          2.FEATURE_CONTEXT_M ...

  7. codevs 1222 信与信封问题

    /* 二分图 题目给出的是确定不连通的边 如果我们拿剩下的可能联通也可能不连通的边跑最大匹配 如果不是完美非配 也就是说把所有可能的边都认为是一定的 这样都跑不出来(不能匹配到每个点)那么一定不能确定 ...

  8. INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 错误

    在eclipse编译好文件之后,往AVD中安装apk,报错如下:INSTALL_PARSE_FAILED_MANIFEST_MALFORMED一般来说只需要检查AndroidManifest.xml中 ...

  9. GridView布局及适配器优化

    1.布局样式 <GridView android:id="@+id/gridView" android:layout_width="fill_parent" ...

  10. 一个类实现多个接口的demo

    //A接口 interface A{ public int getA(); } //B接口 interface B{ public int getB(); } //实现了某个接口必须实现其全部的方法 ...