A.中位数

思路:二分check是否存在>=mid的中位数,若sum[j]-sum[i]>0 && j-i+1>=len 则一定存在大于等于k的mid

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const ll MOD=1e9+7;
const int Seed=1e7+7;
template <class T>
bool sf(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
ll a[N];
ll b[N];
ll sum[N];
ll mn[N];
int n,k;
bool check(ll mid){
    for(int i=1;i<=n;i++){
        if(a[i]<mid)    b[i]=-1;
        else    b[i]=1;
    }
    for(int i=1;i<=n;i++)   sum[i]=sum[i-1]+b[i];
    mn[1]=sum[1];
    for(int i=2;i<=n;i++)   mn[i]=min(mn[i-1],sum[i]);
    for(int i=1;i<=n;i++){
        if(i+1-k<=0)  continue;
        if(sum[i]-mn[i-k]>0){
//            printf("sum[%d]=%d mn[%d]=%d\n",i,sum[i],i+1-k,sum[i+1-k]);
            return true;
        }
    }
    return false;
}
/*
5 2
2 3 4 2 5
*/
int main(){
    sf(n),sf(k);
    for(int i=1;i<=n;i++)   sf(a[i]);
    ll l=1,r=1e9;
    ll ans;
//    cout << check(5) << endl;
    while(l<=r){
        ll mid=l+r>>1;
//        cout << mid<<" "<<check(mid) << endl;
        if(check(mid))  ans=mid,l=mid+1;
        else    r=mid-1;
    }
    printf("%lld\n",ans);

    return 0;
}