Advertisement
Guest User

Untitled

a guest
May 6th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.33 KB | None | 0 0
  1. var n,m,i,j,k,cnt,lift,sum:longint;
  2.     a:array[0..200001] of longint;
  3.    used:array[0..200001] of boolean;
  4.    och:array[0..200001] of longint;
  5. procedure swap(Var t,y:longint);
  6. var u:longint;
  7. begin
  8. u:=t; t:=y; y:=u;
  9. end;
  10.  
  11. procedure sort(l,r:longint);
  12. var i,j,x:longint;
  13. begin
  14.  x:=a[random(r-l)+l];
  15.  i:=l; j:=r;
  16.  repeat
  17.  while a[i]<X do inc(i);
  18.  while a[j]>x do dec(j);
  19.  if i<=j then
  20.  begin
  21.   swap(A[i],a[j]);
  22.   inc(i);
  23.   dec(j);
  24.  end;
  25. until i>j;
  26. if l<j then sort(l,j);
  27. if i<r then sort(i,r);
  28. end;
  29.  
  30.  
  31.  
  32.  
  33. begin
  34.  randomize;
  35.  readln(n,m);
  36.  for i:= 1 to n do
  37.  read(A[i]);
  38.  sort(1,n);
  39.  i:=1;
  40.  j:=n;
  41.  cnt:=0;
  42.  fillchar(used,sizeof(used),true);
  43.  while (i<j) do
  44.   begin
  45.    if a[i]+a[j]<=m then i:=i+1 else
  46.    begin
  47.     inc(cnt);
  48.     och[cnt]:=a[i];
  49.     inc(cnt);
  50.     och[cnt]:=a[j];
  51.     used[i]:=false;
  52.     used[j]:=false;
  53.     i:=i+1;
  54.     j:=j-1;  
  55.   end;
  56.  end;
  57.  for i:= n downto 1 do
  58.   if used[i] then
  59.     begin      
  60.        inc(cnt);
  61.        och[cnt]:=a[i];
  62.     end;
  63.  lift:=0;
  64.  sum:=0;
  65.  cnt:=0;
  66.  for i:= 1 to n do
  67.  begin
  68.   if (sum+och[i]>m) or (Cnt=2) then
  69.     begin
  70.      lift:=lift+1;
  71.      sum:=och[i];
  72.      cnt:=1;
  73.     end else
  74.   begin
  75.   sum:=sum+och[i];
  76.   cnt:=cnt+1;
  77.   end;
  78.  end;
  79.  if sum>0 then lift:=lift+1;
  80.  writeln(lift);
  81.  for i:= 1 to n do
  82.  write(och[i],' ');
  83.  writeln;
  84. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement