BZOJ1629: [Usaco2007 Demo]Cow Acrobats

时间:2022-03-23 23:57:50

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&colon; &lbrack;Usaco2007 Demo&rsqb;Cow Acrobats

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

  2. 【BZOJ】1629&colon; &lbrack;Usaco2007 Demo&rsqb;Cow Acrobats(贪心&plus;排序)

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

  3. &lbrack;USACO2007 Demo&rsqb; Cow Acrobats

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

  4. bzoj 1629&colon; &lbrack;Usaco2007 Demo&rsqb;Cow Acrobats【贪心&plus;排序】

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

  5. 【刷水-贪心】BZOJ1629-&lbrack;Usaco2007 Demo&rsqb;Cow Acrobats

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

  6. BZOJ1631&colon; &lbrack;Usaco2007 Feb&rsqb;Cow Party

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

  7. BZOJ1697&colon; &lbrack;Usaco2007 Feb&rsqb;Cow Sorting牛排序

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

  8. POJ 3045 Cow Acrobats &lpar;贪心&rpar;

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

  9. BZOJ1638&colon; &lbrack;Usaco2007 Mar&rsqb;Cow Traffic 奶牛交通

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

随机推荐

  1. CSS导航菜单水平居中的多种方法

    CSS导航菜单水平居中的多种方法 在网页设计中,水平导航菜单使用是十分广泛的,在CSS样式中,我们一般会用Float元素或是「display:inline-block」来解决.而今天主要讲解如何让未知 ...

  2. &lbrack;Objective-C&rsqb;关联&lpar;objc&lowbar;setAssociatedObject、objc&lowbar;getAssociatedObject、objc&lowbar;removeAssociatedObjects)

    关联 关联是指把两个对象相互关联起来,使得其中的一个对象作为另外一个对象的一部分.    关联特性只有在Mac OS X V10.6以及以后的版本上才是可用的. 在类的定义之外为类增加额外的存储空间 ...

  3. JSPatch中的OC高级语法

    1)多线程相关 dispatch_after dispatch_async dispatch_sync dispatch_get_main_queue dispatch_get_global_queu ...

  4. 对于JAVA课程的期望

    对于JAVA课程的期望 我对于JAVA这门课程最初的了解可能来自于学长学姐的描述,或者是选课指南上简单的课程名称,那个时候的JAVA,对我来说遥远而又陌生,显得那么高大上,但是一转眼自己马上就要结束大 ...

  5. oracle object&lowbar;id和data&lowbar;object&lowbar;id的区别

    Oracle的数据字典表dba_objects包含了两个字段,object_id, data_object_id,官方文档上的解释是: object_id: Dictionary object num ...

  6. 27 Best Free Eclipse Plug-ins for Java Developer to be Productive

    Eclipse offers an integrated development environment having an extensible plug-in system. This enabl ...

  7. LuaNode 开源库

    受CJSON库的启发,用C++实现解析Lua文件的库. 整个库大概800行代码,因为新鲜出炉,所以有些潜在问题尚未发现. 截图中包含使用例子. 以下接口清单: LuaNode(); LuaNode(c ...

  8. Linux 统计文件夹下文件个数

    查看统计当前目录下文件的个数,包括子目录里的. ls -lR| grep "^-" | wc -l Linux下查看某个目录下的文件.或文件夹个数用到3个命令:ls列目录.用gre ...

  9. 设计模式总结篇系列:装饰器模式(Decorator)

    在面向对象设计过程中,经常会遇到需要对现有的类的功能进行扩展,通常我们可以采用继承的方式.例如老罗最近在做手机,一开始需要定义手机所应具有的功能: interface Phone{ public vo ...

  10. 洛谷 P1878 舞蹈课 解题报告

    P1878 舞蹈课 题目描述 有\(n\)个人参加一个舞蹈课.每个人的舞蹈技术由整数来决定.在舞蹈课的开始,他们从左到右站成一排.当这一排中至少有一对相邻的异性时,舞蹈技术相差最小的那一对会出列并开始 ...