危险系数题解

先按每个人的力量和重量的和来个快排,从小到大。至于为什么是力量+重量,等会再说。

然后按题目描述的,从1到n累加重量,计算危险系数,求最大值。

考虑两个人的叠罗汉。设第一个人的重量为w1,力量为s1。第二个人的重量为w2,力量为s2。

如果让第一个人在上面并保证危险系数最小,那么则需要满足:

w1-s2<w2-s1

我们可以把它转化成

w1+s1<w2+s2

代码如下

var
  a,b:array[0..100000]of int64;
  i,j:longint;
  tz,xs,n:int64;
procedure js(l,r:int64);
  var
    i,j:int64;
    m,t:int64;
  begin
    m:=a[(l+r)div 2]+b[(l+r)div 2];i:=l;j:=r;
    repeat
     while a[i]+b[i]<m do inc(i);
     while a[j]+b[j]>m do dec(j);
     if i<=j then
      begin
       t:=a[i];a[i]:=a[j];a[j]:=t;
       t:=b[i];b[i]:=b[j];b[j]:=t;
       inc(i);dec(j);
      end;
    until i>j;
    if l<j then js(l,j);
    if i<r then js(i,r);
  end;
begin
  read(n);
  for i:=1 to n do read(a[i],b[i]);
  js(1,n);
  xs:=-maxlongint;
  for i:=1 to n do
   begin
    if tz-b[i]>xs then xs:=tz-b[i];
    tz:=tz+a[i];
   end;
  write(xs);
end.

猜你喜欢

转载自blog.csdn.net/chenchenfcs/article/details/52204654