题面:

 

 

思路:

 

代码:

var i,j,n,m,x,y,tail,tt:longint;
 a,q,f,id,b:array[0..500005] of longint;
  ans:int64;

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

 begin

 assign(input,'patrik.in');
 reset(input);
 assign(output,'patrik.out');
 rewrite(output);

 read(n);
 for i:=1 to n do begin read(b[i]); id[i]:=i; end;
 sort(1,n);
 tt:=0;
 for i:=1 to n do
  if b[i]=b[i-1] then a[id[i]]:=tt else begin inc(tt); a[id[i]]:=tt; end;
 tail:=1; q[1]:=a[1]; f[a[1]]:=1;
 for i:=2 to n do
  begin
   while (a[i]>q[tail]) and (1<=tail) do begin dec(f[q[tail]]); dec(tail); inc(ans); end;
   inc(tail); q[tail]:=a[i]; inc(ans,f[a[i]]); inc(f[a[i]]);
   if q[1]<>a[i] then inc(ans);
  end;

 writeln(ans);


close(input);close(output);


end.

 

posted on 2018-10-04 14:06  Tolye  阅读(123)  评论(0编辑  收藏  举报