Greedy:Cow Acrobats(POJ 3045)

           

                牛杂技团

  题目大意:一群牛想逃跑,他们想通过搭牛梯来通过,现在定义risk(注意可是负的)为当前牛上面的牛的总重量-当前牛的strength,问应该怎么排列才能使risk最小?

  说实话这道题我一开始给书上的二分法给弄懵了,后来看了一下题解发现根本不是这么一回事,其实就是个简单的贪心法而已。

  这题怎么贪心呢?就是按w+s从小到大排序就好了,证明一下:

  1.先证明如果不满足w+s的序列性,无论谁在谁的上面,都会违反题设:(设A在B的上面)

    如果 A.s+A.w<B.s+B.w

    则如果B.s<m+A.w

    则如果B在A的上面A.s<B.s+B.w-A.w=B.w+m

    得证

  2.再证明一下如果采用排序的确能让risk的最大值最小。

    如果 A.s+A.w<B.s+B.w

    ①A在B的上面

    riskA1=m-A.s  riskB1=m+A.w-B.s

    ②B在A的上面

    riskB2=m-B.s  riskA2=m+B.w-A.s

    所以riskA2>riskA1

      另外riskB1-riskA2=A.w+A.s-B.w-B.s<0  所以riskA2>riskB1>riskB2

    则违反后risk会产生一个比三个risk更大的数,不符合题意

    参考http://poj.org/showmessage?message_id=341726

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <functional>
 4
 5 using namespace std;
 6 typedef long long LL_INT;
 7
 8 typedef struct _cows
 9 {
10     LL_INT strength;
11     LL_INT weight;
12     bool operator<(const  _cows &x) const
13     {
14         return strength + weight > x.weight + x.strength;
15     }
16 }Cows;
17
18 static Cows cows_set[50001];
19 void Search(const int, LL_INT);
20 bool judge(const LL_INT, const int,const LL_INT);
21
22 int main(void)
23 {
24     int sum_cows;
25     LL_INT sum_w;
26
27     while (~scanf("%d", &sum_cows))
28     {
29         sum_w = 0;
30         for (int i = 0; i < sum_cows; i++)
31         {
32             scanf("%lld%lld", &cows_set[i].weight, &cows_set[i].strength);
33             sum_w += cows_set[i].weight;
34         }
35         sort(cows_set, cows_set + sum_cows);
36         Search(sum_cows,sum_w);
37     }
38     return 0;
39 }
40
41 void Search(const int sum_cows, LL_INT sum_w)
42 {
43     LL_INT ans = -1000000001;
44
45     for (int i = 0; i < sum_cows; i++)
46     {
47         sum_w -= cows_set[i].weight;
48         ans = max(ans, sum_w - cows_set[i].strength);
49     }
50     cout << ans << endl;
51 }

其实这题的思想和Protecting Flowers那题有点像,都是只看两个元素之间的两个量之间的练联系,而不只是单单的一个量

时间: 2024-10-13 02:36:01

Greedy:Cow Acrobats(POJ 3045)的相关文章

Cow Acrobats POJ - 3045

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 wit

POJ 3045 Cow Acrobats (想法题)

题目链接:POJ 3045 Cow Acrobats 题意:有n只牛叠罗汉,危险指数的计算是 该层牛以上的牛重量总和减去这层牛的强度,求使最大的危险指数中的最小值. 思路:根据w+s排序,最大的在最下面,道理很简单,危险指数: sum-(w+s),(sum该层牛以上的牛重量总和). AC代码: #include<stdio.h> #include<string.h> #include<algorithm> #define ll __int64 using namespa

POJ3045 Cow Acrobats —— 思维证明

题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5713   Accepted: 2151 Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. The

[USACO2005][POJ3045]Cow Acrobats(贪心)

题目:http://poj.org/problem?id=3045 题意:每个牛都有一个wi和si,试将他们排序,每头牛的风险值等于前面所有牛的wj(j<i)之和-si,求风险值最大的牛的最小风险值 分析:这就是noip2012 T2的来源= =只不过这里是加,noip里是乘 不妨设所有牛都按最优顺序排好了,考虑相邻的两头牛i和i+1,如果交换他们的位置,那么对前面和后面的结果都无影响,只是他们两个的风险值变化了(变大了),于是我们可以得到这个时候i和i+1的关系 设w1+w2+...+wi-1

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

【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 p

Cow Acrobats(贪心)

Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3686   Accepted: 1428 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 tig

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

poj 3045 Cow Acrobats(数学题)

题目链接:http://poj.org/problem?id=3045 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