• 牛客NOIP提高组R1 A中位数(二分)


    题意

    题目链接

    Sol

    很神仙的题目啊,考场上只会$n^2$的暴力。。

    考虑直接二分一个$mid$,我们来判断最终答案是否可能大于$x$。

    判断的时候记录一下前缀最小值即可,

    设$s[i]$表示$1-i$中有多少比它大的,要求的长度为$len$,我们记下$s[i - len]$的最小值为$Mi$

    若$s[i] - Mi > 0$,那么说明在长度至少为$len$的区间中,大于$mid$的数和小于$mid$的数相互抵消后仍然有比$mid$大的数,此时$mid$是合法的

    第一次做这种二分答案,但答案不是给出的数的题。神仙啊qwq

    /*
     
    */
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<vector>
    #include<set>
    #include<queue>
    #include<cmath>
    #define Pair pair<int, int>
    #define MP(x, y) make_pair(x, y)
    #define fi first
    #define se second
    #include<set>
    #include<vector>
    //#define int long long
    #define LL long long
    //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
    //char buf[(1 << 22)], *p1 = buf, *p2 = buf;
    using namespace std;
    const int MAXN = 1e5 + 10, INF = 1e9 + 10, mod = 1e9 + 7;
    const double eps = 1e-9;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N, len, s[MAXN], a[MAXN];
    int check(int x) {
        for(int i = 1; i <= N; i++)
            if(a[i] < x) s[i] = -1; 
            else s[i] = 1;//s[i] :  1 - i中有多少比x大的 
        int Mi = 0;
        for(int i = 1; i <= N; i++) {
            s[i] += s[i - 1];
            if(i >= len) Mi = min(Mi, s[i - len]);
            if(i >= len && (s[i] - Mi > 0)) return 1;
        }
        return 0;
    } 
    int main() {
    //    freopen("a.in", "r", stdin);
    //    freopen("c.out", "w", stdout);
        N = read(); len = read();
        for(int i = 1; i <= N; i++) a[i] = read();
        int l = 0, r = 1e9 + 10, ans;
        while(l <= r) {
            int mid = l + r >> 1;
            if(check(mid)) ans = mid, l = mid + 1;//是否有比mid大的解 
            else r = mid - 1;
        }
        printf("%d", ans);
        return 0;
    }
    /*
    5 4
    7 2 3 2 6 
    */
  • 相关阅读:
    Linux对文件的权限管理
    在Eclipse中安装TestNG
    JUnit 4 与 TestNG 对比
    postman之HTTP请求
    Fiddler抓包后保存为JMX(jmeter脚本,不限jmeter使用版本)
    JMeter使用之BlazeMeter的安装及初步使用
    Postman的第一个案例演示
    Postman的安装及注意事项
    SVN学习记录
    TestNG中如何执行测试
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9614078.html
Copyright © 2020-2023  润新知