• BZOJ 1303: [CQOI2009]中位数图 问题转化_扫描_思维


    将比 b 大的设成 1,比 b 小的设成 -1,开个桶左右扫描一下,乘法原理乘一乘就好了。

    虽然一眼切,不过这个基于中位数的转化还是相当重要的。middle 那个主席树的题也需要该做法

    Code:

    #include <bits/stdc++.h>
    #define setIO(s) freopen(s".in","r",stdin) 
    #define maxn 1000000 
    using namespace std;
    int arr[maxn],neg1[maxn],pos1[maxn],neg2[maxn],pos2[maxn],cur=0,zero1,zero2; 
    int main(){
        // setIO("input"); 
        int n,k; 
        long long ans = 1; 
        scanf("%d%d",&n,&k); 
        for(int i = 1,a;i <= n; ++i) {
            scanf("%d",&a),arr[i]=(a<k)?-1:(a==k?0:1); 
            if(a==k) cur=i; 
        }   
        if(cur >= 1)
        {
            for(int i = cur-1,tot=0;i>=1;--i)  
            {
                tot+=arr[i];
                if(tot > 0) ++pos1[tot]; 
                if(tot < 0) ++neg1[-tot]; 
                if(tot==0) ++ans,++zero1; 
            }
        }
        if(cur <= n) 
        {
            for(int i = cur+1,tot=0;i<=n;++i) 
            {
                tot+=arr[i]; 
                if(tot > 0) ++pos2[tot];
                if(tot < 0) ++neg2[-tot];
                if(tot == 0) ++ans, ++zero2; 
            }
        }
        ans += (long long)zero1*zero2; 
        for(int i = 1;i <= n; ++i) 
        {
            ans += (long long)pos1[i]*neg2[i]; 
            ans += (long long)neg1[i]*pos2[i]; 
        }
        printf("%lld",ans); 
        return 0; 
    }
    

      

  • 相关阅读:
    开源数据汇集工具
    scrapy定时执行抓取任务
    xpath的常见操作
    ubuntu 安装python mysqldb
    sudo: /etc/sudoers is owned by uid 755, should be 0
    ubuntu 14.04安装mysql数据库
    win7 远程桌面连接centos 6.5
    本地启动spark-shell
    ubuntu 安装 2.10.x版本的scala
    unfolding maps支持中文
  • 原文地址:https://www.cnblogs.com/guangheli/p/10942267.html
Copyright © 2020-2023  润新知