↓ Skip to main content
  1. Posts/

BZOJ 1629: [Usaco2007 Demo]Cow Acrobats (贪心)

·778 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

1629: [Usaco2007 Demo]Cow Acrobats
#

Time Limit: 5 Sec Memory Limit: 64 MB Submit: 771 Solved: 398 [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 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.

思路:贪心。两头奶牛的相对位置对于其他奶牛是没有影响的,因为这两头奶牛的体重之和一定。

那么对于两头奶牛i,j,谁放在下面呢?

如果 i 放在下面,代价为 s[i]-w[j];如果 j 放在下面,代价为 s[j]-w[i]。

当且仅当 s[i]-w[j]<s[j]-w[i],也就是 s[i]+w[i]<s[j]+w[j] 时,i 放在下面。

所以以 s[i]+w[i] 的和为关键字排序就好。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月04日 星期一 17时12分44秒
 4File Name :code/bzoj/1629.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=5E4+7;
34int n;
35struct node
36{
37    int s,w;
38
39    bool operator < (node b)const
40    {
41	return w+s<b.w+b.s;
42    }
43}a[N];
44int main()
45{
46	#ifndef  ONLINE_JUDGE
47	freopen("code/in.txt","r",stdin);
48  #endif
49
50	ios::sync_with_stdio(false);
51	cin>>n;
52	for ( int i = 1 ; i <= n ; i++) cin>>a[i].w>>a[i].s;
53
54	sort(a+1,a+n+1);
55
56	int ans = -inf;
57	int total =  0;
58	for ( int i = 1 ; i <= n ; i++)
59	{
60	    ans = max(ans,total-a[i].s);
61	    total += a[i].w;
62	}
63
64	cout<<ans<<endl;
65
66
67  #ifndef ONLINE_JUDGE
68  fclose(stdin);
69  #endif
70    return 0;
71}

Related

BZOJ 1623: [Usaco2008 Open]Cow Cars 奶牛飞车 (贪心)

·811 words·2 mins
1623: [Usaco2008 Open]Cow Cars 奶牛飞车 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 386 Solved: 266 [Submit][Status][Discuss] Description # 编号为1到N的N只奶牛正各自驾着车打算在牛德比亚的高速公路上飞驰.高速公路有M(1≤M≤N)条车道.奶牛i有一个自己的车速上限Si(l≤Si≤1,000,000).

BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理 (贪心)

·633 words·2 mins
1620: [Usaco2008 Nov]Time Management 时间管理 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 636 Solved: 387 [Submit][Status][Discuss] Description # Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. He has N jobs conveniently numbered 1..N (1 <= N <= 1,000) to accomplish (like milking the cows, cleaning the barn, mending the fences, and so on). To manage his time effectively, he has created a list of the jobs that must be finished. Job i requires a certain amount of time T_i (1 <= T_i <= 1,000) to complete and furthermore must be finished by time S_i (1 <= S_i <= 1,000,000). Farmer John starts his day at time t=0 and can only work on one job at a time until it is finished. Even a maturing businessman likes to sleep late; help Farmer John determine the latest he can start working and still finish all the jobs on time.

codeforces 137 C. History (sorting,贪心)

·343 words·1 min
题目链接 题意:给出n个时间的开始和截止时间,保证没有两个时间的开始或者截止时间相同,问有多少个时间被包含在其他事件中。即aj < ai and bi < bj. 思路:没有两个事件的时间相同很关键。 那么我们可以直接按照开始时间为关键字排序,然后结束时间取之前发生了的(可能还没发生完)时间的结束时间的最大值即可。

hdu 5646 ||bc #76 div 2 (贪心)

·845 words·2 mins
题目链接 题意:将正整数n(n<=1E9),拆分成k个(k<=1E9)个**互不相等的正整数,**并且使得k个正整数的乘积最大。如果可以拆分,输出最大乘积,否则输出-1.

codeforces croc 2016 B. Mischievous Mess Makers (贪心)

·282 words·1 min
题目链接 题意:长度为n的初始为1,2,3…n的序列,最多进行k次两个数交换,变换后的序列中最懂能有多少逆序对。 思路:贪心得想。。每次变最外层的对答案贡献最多。 以及,最能能变化n/2次。