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.
  • 相关阅读:
    2030年的10大热门职业
    10种散发着爱情信号的肢体语言
    猎头不来找你的5种原因
    15个不得不去的“秘密”景点
    保持微笑的五大好处
    vscode 格式化突然失效
    openlayers之全屏控件的使用
    openlayers之点,线,面(以城市,河流,省份为例,分别对应点线面)
    openlayers 添加标记点击弹窗 定位图标闪烁
    搜索框focus 搜索面板显示 点击别处消失 从浏览器别的页面回来消失
  • 原文地址:https://www.cnblogs.com/BLADEVIL/p/3511936.html
Copyright © 2011-2022 走看看