Thứ Hai, 9 tháng 5, 2022

BCFRIEND PASCAL

var
   a:array [0..1000000] of longint;
   n,m,b:longint;res:qword;
   d:array[0..100000] of longint;


procedure nhap;
var i:longint;
begin
    readln(n,b);
    for i:=1 to n do readln(a[i]);
end;

procedure sort(l,r:longint);
   var i,j,m,x,y:longint;
begin
   i:=l;j:=r;
   m:=random(r-l)+l+1;
   x:=a[m];
   repeat
      while a[i]<x do inc(i);
      while a[j]>x do dec(j);
      if i<=j then
      begin
         y:=a[i];a[i]:=a[j];a[j]:=y;
         inc(i);dec(j);
      end;
   until i>j;
   if i<r then sort(i,r);
   if l<j then sort(l,j);
end;

function chatmax(l,r,x:longint):longint;
var m,res:longint;
begin
    res:=0;
    while l<=r do
    begin
        m:=(l+r) div 2;
        if (a[m]<=x) then
        begin
            res:=m;
            l:=m+1;
        end
        else r:=m-1;
   end;
   if a[res]=x then exit(res)
   else exit(0);
end;

function chatmin(l,r,x:longint):longint;
var m,res:longint;
begin
    res:=0;
    while l<=r do
    begin
        m:=(l+r) div 2;
        if a[m]>=x then
        begin
            res:=m;
            r:=m-1;
        end
        else l:=m+1;

    end;
    if a[res]=x then exit(res)
    else exit(0);
end;

procedure xuli;
var i,j,x,y:longint;
begin
    sort(1,n);
    res:=0;
    for i:=1 to n-1 do
    begin
        x:=chatmin(i+1,n,b-a[i]);
        y:=chatmax(i+1,n,b-a[i]);
        if (x<>0) then res:=res+(y-x+1);
    end;
    writeln(res);
end;

begin
assign(input,'bcfriend.inp');reset(input);
assign(output,'bcfriend.out');rewrite(output);
nhap;
giai;
end.

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.