1486D - Max Median 2100

题目来源

https://codeforces.ml/problemset/problem/1486/D

题意分析

  在给出的数组中,找出所有长度大于等于k的序列中的中位数的最大值。

思路分析

  [待补充]

code

#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 2e5 + 7;
ll pre[maxn];
int a[maxn];
int n, k;

bool check(int mid){
    for (int i=1; i<=n; i++){
        if (a[i] >= mid) pre[i] = 1;
        else pre[i] = -1;
    }
    ll mmin = INF;
    for (int i=1; i<=n; i++) pre[i] += pre[i-1];
    for (int i=k; i<=n; i++){
        int l = i - k, r = i;
        mmin = min(mmin, pre[l]);
        if (r > n) break;
        if (pre[r] - mmin > 0)  return true;

    }
    return false;
}

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

 

 

posted @ 2021-03-20 12:01  Rain_island  阅读(33)  评论(0编辑  收藏  举报
Title