比赛 20090927练习赛 评测结果 AAAAAAAAAA
题目名称 排序工作量 最终得分 100
用户昵称 lc 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2009-09-27 21:21:03
显示代码纯文本
program sortt;
 const
   maxn = 10010;
   maxnode = 10010;
   Inf = 65536;
   eps = 1e-7;
 var
   n:   longint;
   ans: longint;
   A:   array[1..maxn] of double;
   root,treenode:       longint;
   tree:        array[0..maxnode] of record
                key:                        double;
                pro,count,size,left,right:  longint;

                end;



function cmp(x:double):longint;

 begin
   if abs(x) <eps then exit(0)
   else
   if x <0 then exit(-1) else exit(1);
 end;


procedure init;
 var
   i:   longint;
 begin
   readln(n);
   randomize;
   for i :=1 to n do read(A[i]);
   tree[0].pro :=Inf;
 end;


procedure update(T:longint);

 begin
   tree[T].size :=tree[tree[T].left].size + tree[T].count + tree[tree[T].right].size;
 end;


procedure RightRoate(var T:longint);
 var
   P:   longint;
 begin
   P :=tree[T].right;
   tree[T].right :=tree[P].left;
   tree[P].left :=T;
   update(T);
   update(P);
   T :=P;
 end;


procedure LeftRoate(var T:longint);
 var
   P:   longint;
 begin
   P :=tree[T].left;
   tree[T].left :=tree[P].right;
   tree[P].right :=T;
   update(T);
   update(P);
   T :=P;
 end;


procedure Insert(var root:longint; x:double);

 begin
   if root = 0 then
      begin
      inc(treenode);
      tree[treenode].key :=x;
      tree[treenode].pro :=random(Inf);
      tree[treenode].count :=1;
      tree[treenode].size :=1;
      tree[treenode].left :=0;
      tree[treenode].right :=0;
      root :=treenode;
      exit;
      end;

   if cmp(x-tree[root].key) = 0
      then inc(tree[root].count)
   else
   if cmp(x-tree[root].key) <0
      then begin
           Insert(tree[root].left,x);
           if tree[tree[root].left].pro <tree[root].pro
              then LeftRoate(root);
           end
      else begin
           Insert(tree[root].right,x);
           if tree[tree[root].right].pro <tree[root].pro
              then RightRoate(root);
           end;

   update(root);
 end;

procedure total(root:longint; x:double);

 begin
   while root <>0 do begin
     if cmp(x - tree[root].key) <0
        then begin
             inc(ans,tree[root].size - tree[tree[root].left].size);
             root :=tree[root].left;
             end
        else root :=tree[root].right;
     end;
 end;


procedure main;
 var
   i:   longint;
 begin
   Insert(root,A[1]);
   for i :=2 to n do begin
       total(root,A[i]);
       Insert(root,A[i]);
       end;
   writeln(ans);
 end;


begin
  assign(input,'sortt.in');  reset(input);
  assign(output,'sortt.out'); rewrite(output);
  init;
  main;
  close(input);  close(output);
end.