清北学堂模拟赛d6t2 刀塔

分析:看到最小值最大就很显然是二分了吧,二分一下最小值,把小于它的数给删掉,然后看每个数向左边能延伸多长,往右边能延伸多长,最后统计一下有没有可行答案就可以了.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n, a, b, k, x[3000010], r, l, ans, lx[3000010], rx[3000010];

bool check(int u)
{
    memset(lx, 0, sizeof(lx));
    memset(rx, 0, sizeof(rx));
    for (int i = 1; i <= n; i++)
    {
        if (x[i] >= u)
            lx[i] = lx[i - 1] + 1;
        else
            lx[i] = 0;
    }
    for (int i = n; i >= 1; i--)
    {
        if (x[i] >= u)
            rx[i] = rx[i + 1] + 1;
        else
            rx[i] = 0;
    }
    for (int i = 1; i <= n; i++)
        if (lx[i - 1] >= a && rx[i + k] >= a && lx[i - 1] + rx[i + k] >= b)
            return true;
    return false;
}

int main()
{
    scanf("%d%d%d%d", &n, &a, &b, &k);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &x[i]);
        r = max(r, x[i]);
    }
    l = 1;
    r++;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (check(mid))
        {
            ans = mid;
            l = mid + 1;
        }
        else
            r = mid - 1;
    }
    printf("%d\n", ans);

    return 0;
}

 

posted @ 2017-10-06 18:37  zbtrs  阅读(175)  评论(0编辑  收藏  举报