zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    竞态与死锁
    Java-核心技术-面试整理
    接口(工厂模式&代理模式)
    switch case实现两个数的算术运算
    继承和多态举例
    字符串的逆序输出
    引用传递&值传递
    递归的使用
    构造方法的重载
    给定数组,去掉0元素后将剩下的元素赋给新的数组
  • 原文地址:https://www.cnblogs.com/BLADEVIL/p/3511936.html
Copyright © 2011-2022 走看看