码迷,mamicode.com
首页 > 其他好文 > 详细

poj3045Cow Acrobats

时间:2019-07-14 12:48:03      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:++   tun   iat   ems   for   algorithm   can   iostream   rev   

                                                                                                                                                              Cow Acrobats
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7564   Accepted: 2822

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 ithin 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

Hint

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.
这道题呢,其实是国王游戏的简单版https://www.luogu.org/problemnew/show/P1080
同样是一道运用邻项交换的简单贪心
看上去是个水题(其实真的就是水题)
好了,不扯远了,下面我们来看怎么贪
按照每头牛重量强度之和从小到大排
证明:
对于任意一种顺序,设n头牛的重量与强度为w[1]~w[n]与s[1]~s[n]
如果我们交换相邻两只牛的位置,在交换前的崩溃值分别为
i-1                                  i
∑ W[j] -s[i]    与    ∑ W[j] -s[i+1]
j=0                                 j=0
 
                      i-1
两边同时减去∑ W[j] 得    -s[i] 与 w[i]-s[i+1] 
                      j=0   
 
在交换后的崩溃值分别为
i-1                                                      i-1
∑ W[j] -s[i+1]     与    ∑ W[j] -s[i]+w[i+1]
j=0                                                     j=0
 
 
                            i-1
两边同时减去∑ W[j]     -s[i+1] 与 w[i+1]-s[i] 
                            j=0   
将标红得两部分里的各式加上s[i]+s[i+1]
             s[i+1] 与 w[i]+s[i]
     s[i]  与   w[i+1]+s[i+1]
我们要找最优方案,实际上就是要看交换前更优还是交换后更优

所以比较一下 min{s[i+1],w[i]+s[i]}   与  min{s[i],w[i+1]+s[i+1]} 谁更小
显然 s[i+1]<w[i+1]+s[i+1]  s[i]<w[i]+s[i]
所以 w[i]+s[i]<w[i+1]+s[i+1] 左式值小于右式 故交换前更优
 
推了半天终于推出了排序方法,不说了,见code
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<iostream>
 6 using namespace std;
 7 int n;
 8 long long maxx=-999999999,ans=0;
 9 struct node{
10     long long a,w;
11 }cow[50005];
12 bool cmp(node a,node b)
13 {
14     return a.a+a.w<b.a+b.w;
15 }
16 int main()
17 {
18     scanf("%d",&n);
19     for(int i=1;i<=n;i++)
20         scanf("%lld%lld",&cow[i].a,&cow[i].w);
21     sort(cow+1,cow+n+1,cmp);
22     for(int i=1;i<=n;i++)
23     {
24         ans=ans+cow[i-1].a-cow[i].w+cow[i-1].w;
25         maxx=max(maxx,ans);
26     }
27     printf("%lld",maxx);
28     return 0;
29 }

 

poj3045Cow Acrobats

标签:++   tun   iat   ems   for   algorithm   can   iostream   rev   

原文地址:https://www.cnblogs.com/yearning/p/11183584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!