bzoj 1303 杂题

  首先如果一个数是中位数,在这段区间中比他大的数量=比他小的数量,那么如果一个数比他大设为1,比他小设为-1,设要求的数在数组中的位置是mid,那么我们可以用num[i] 表示1-mid这一段中,j-mid的和为i的j的数量。那么我们扫mid到n,假设mid到j的和为a,那么代表这段比他大的有a个,因为要保证数量相等,所以要在num数组里-a的个数累加答案。

/**************************************************************
    Problem: 1303
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time:84 ms
    Memory:1396 kb
****************************************************************/
 
//By BLADEVIL
var
    n, k                    :longint;
    a                       :array[0..100010] of longint;
    num                     :array[-100010..100010] of longint;
    ans                     :int64;
     
procedure init;
var
    i                       :longint;
begin
    read(n,k);
    for i:=1 to n do read(a[i]);
end;
 
procedure main;
var
    i                       :longint;
    mid                     :longint;
    tot                     :longint;
begin
    for i:=1 to n do if a[i]=k then break;
    mid:=i;
    for i:=1 to n do
        if a[i]>k then a[i]:=1 else
        if a[i]<k then a[i]:=-1 else a[i]:=0;
     
    tot:=0;
    for i:=mid downto 1 do
    begin
        tot:=tot+a[i];
        inc(num[tot]);
    end;
    tot:=0;
    for i:=mid to n do
    begin
        tot:=tot+a[i];
        ans:=ans+num[-tot];
    end;
    writeln(ans);
end;
 
begin
    init;
    main;
end.

 

posted on 2014-01-09 11:47  BLADEVIL  阅读(175)  评论(0编辑  收藏  举报