比赛 20111108 评测结果 AAAAAAAAAT
题目名称 数对的个数 最终得分 90
用户昵称 lizhe 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-11-08 11:20:00
显示代码纯文本
program dec;
var
  i,j,n,c,l,r,mid,num,x:longint;
  ans:int64;
  a,t,p:array[1..200000]of longint;
procedure swap(var a0,b0:longint);
var
  c0:longint;
begin
  c0:=a0;
  a0:=b0;
  b0:=c0
end;

procedure sort(l,r:longint);
var
  i,j,x:longint;
begin
  i:=l;
  j:=r;
  x:=p[l];
  repeat
    while (p[j]>=x) and (i<j) do j:=j-1;
    swap(p[i],p[j]);
    while (p[i]<=x) and (i<j) do inc(i);
    swap(p[i],p[j])
  until i=j;
  inc(i); j:=j-1;
  if i<r then sort(i,r);
  if l<j then sort(l,j)
end;

procedure init;
begin
  assign(input,'dec.in');
  reset(input);
  assign(output,'dec.out');
  rewrite(output);
  read(n,c);
  for i:=1 to n do
    read(p[i]);
  sort(1,n);
  a[1]:=p[1];
  x:=a[1];
  t[1]:=1;
  num:=1;
  for i:=2 to n do
    if p[i]=x then
      inc(t[num])
    else
    begin
      inc(num);
      a[num]:=p[i];
      inc(t[num]);
      x:=p[i]
    end
end;

procedure erfen;
begin
  l:=1; r:=num;
  while l<r do
  begin
    mid:=(l+r) div 2;
    if a[mid]>=x then r:=mid
    else l:=mid+1
  end;
  if a[l]=x then
    ans:=ans+t[i]*t[l];
end;

procedure main;
begin
  for i:=1 to num do
  begin
    x:=a[i]+c;
    erfen
  end
end;

procedure print;
begin
  writeln(ans);
  close(input);
  close(output)
end;

begin
  init;
  main;
  print
end.