POJ 3045

题意:这道题居然和今年成都赛区的倒数第二题一模一样。。。或者说该反过来说、、给你n头牛叠罗汉,每头都有自己的重量w和力量s,承受的风险数就是该牛上面牛的总重量减去它的力量,题目要求设计一个方案使得所有牛里面风险最大的要最小。

题解:按照w+s贪心叠,越大的越在下面。如果最优放置时,相邻两头牛属性分别为w1,s1,w2,s2,第一头牛在第二头上面,sum为第一头牛上面的牛的体重之和,那么

第一头牛风险:a=sum-s1;第二头牛风险:b=sum+w1-s2;交换两头牛位置之后

a'=sum+w2-s1,b'=sum-s2,

由于是最优放置,所以w2-s1>=w1-s2,即w2+s2>=w1+s1,所以和最大的就该老实的在下面呆着= =!

View Code
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 using namespace std;

 5 struct data

 6 {

 7     int x,y;

 8     bool operator<(const data &ne)const

 9     {

10         return x+y<ne.x+ne.y;

11     }

12 }po[60000];

13 int sum[60000];

14 int main()

15 {

16     int n;

17     while(scanf("%d",&n)!=EOF)

18     {

19         for(int i=1;i<=n;i++)

20             scanf("%d%d",&po[i].x,&po[i].y);

21         sort(po+1,po+n+1);

22         sum[0]=0;

23         int ans=-(1<<29);;

24         for(int i=1;i<=n;i++)

25         {

26             sum[i]=sum[i-1]+po[i].x;

27             ans=max(ans,sum[i-1]-po[i].y);

28         }

29         printf("%d\n",ans);

30     }

31     return 0;

32 }

你可能感兴趣的:(poj)