【BZOJ 1629】 [Usaco2007 Demo]Cow Acrobats

1629: [Usaco2007 Demo]Cow Acrobats

Time Limit: 5 Sec  Memory Limit: 64 MB

Submit: 657  Solved: 331

[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

Silver

贪心。

这道题与【noip2012】国王游戏 几乎一样。

首先要明白对于第i个,放在他上面的第j,k个交换顺序对他没有任何影响。

现在可以只考虑第j,k个:

j放在k的上面,当且仅当 wj-sk<wk-sj  移项得 wj+sj<wk+sk

因此我们只要按照wi+si升序排列,依次放置即可。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n;
struct data
{
	int x,y,s;
}a[50005];
bool cmp(data a,data b)
{
	return a.s<b.s;
}
int main()
{
        scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d%d",&a[i].x,&a[i].y),a[i].s=a[i].x+a[i].y;
	sort(a+1,a+1+n,cmp);
	int sum=a[1].x,ans=-a[1].y;
	for (int i=2;i<=n;i++)
		ans=max(ans,sum-a[i].y),sum+=a[i].x;
	cout<<ans<<endl;
	return 0;
}

时间: 2024-10-14 13:58:42

【BZOJ 1629】 [Usaco2007 Demo]Cow Acrobats的相关文章

1629: [Usaco2007 Demo]Cow Acrobats

1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1023  Solved: 531[Submit][Status][Discuss] Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofe

bzoj1629[Usaco2007 Demo]Cow Acrobats*

bzoj1629[Usaco2007 Demo]Cow Acrobats 题意: n头牛,每天牛都有体重与力量值.它们玩叠罗汉的游戏,每个牛的危险值等于它上面的牛的体重总和减去它的力量值,求所有方案中危险值最大的最小. 题解: 贪心.第i头牛比第j头牛高当且仅当i的重量-j的力量<j的重量-i的力量. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn

【刷水-贪心】BZOJ1629-[Usaco2007 Demo]Cow Acrobats

[题目大意] 有n个头牛,给出体重和力量.每个牛的危险值等于它上面的牛的体重总和减去它的力量值,求所有方案中危险值最大值的最小值. [思路] 贪心.一开始脑补的贪心是体重大的先放下面,体重相同的根据力量值来排.但是其实是不对的QAQ 这里有详细证明: 首先要想到,对于相邻的两头牛,交换它们的位置,仅仅会影响他们两个的risk值 然后,对于最优系列的相邻的两头牛 w1 s1 w2 s2 最顶上的那头的顶上的牛的质量和为sum. 那么第一头牛的risk就是 sum - s1--r1 第二头的为sum

【BZOJ 1690】 [Usaco2007 Dec]奶牛的旅行

1690: [Usaco2007 Dec]奶牛的旅行 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 622  Solved: 327 [Submit][Status] Description 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的城市地图,上面标注了城市中所有L(2 <= L <= 1000)座标志性建筑物(建筑

BZOJ 1629: [Usaco2007 Demo]Cow Acrobats

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 ca

【BZOJ 1627】 [Usaco2007 Dec]穿越泥地

1627: [Usaco2007 Dec]穿越泥地 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 511  Solved: 332 [Submit][Status] Description 清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶.前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞的土地.FJ的屋子在平面坐标(0, 0)的位置,贝茜所在的牛棚则位于坐标(X,Y) (

【BZOJ 1712】 [Usaco2007 China]Summing Sums 加密

1712: [Usaco2007 China]Summing Sums 加密 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 347 Solved: 129 [Submit][Status][Discuss] Description 那N只可爱的奶牛刚刚学习了有关密码的许多算法,终于,她们创造出了属于奶牛的加密方法.由于她们并不是经验十足,她们的加密方法非常简单:第i只奶牛掌握着密码的第i个数字,起始的时候是Ci(0≤Ci<90000000).加密的时

【BZOJ 1642】 [Usaco2007 Nov]Milking Time 挤奶时间

1642: [Usaco2007 Nov]Milking Time 挤奶时间 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 590  Solved: 337 [Submit][Status][Discuss] Description 贝茜是一只非常努力工作的奶牛,她总是专注于提高自己的产量.为了产更多的奶,她预计好了接下来的N (1 ≤ N ≤ 1,000,000)个小时,标记为0..N-1. Farmer John 计划好了 M (1 ≤ M ≤

[USACO2007 Demo] Cow Acrobats

[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1629 [算法] 贪心 考虑两头相邻的牛 , 它们的高度值和力量值分别为ax , ay , bx , by 我们发现 , 当ax + ay < bx + by时 , x排在前面比y排在前面更优 也就是说 , 当序列中有相邻的牛使得ax + ay >= bx + by时 , 可以通过交换两头牛使得答案更优 综上 , 按牛的(高度值 + 力量值)以关键字升序排序 , 即可 时间复杂度