Codeforces Round #703 (Div. 2)D(二分,前缀和)
https://codeforces.com/contest/1486/problem/D
1 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 2 #define bug(x) cout<<#x<<" is "<<x<<endl 3 #include <bits/stdc++.h> 4 #define iter ::iterator 5 using namespace std; 6 typedef long long ll; 7 typedef pair<int,ll>P; 8 #define pb push_back 9 #define mk make_pair 10 #define se second 11 #define fi first 12 #define rs o*2+1 13 #define ls o*2 14 const ll inf=1e18; 15 const int N=2e5+5; 16 int T,n,k; 17 18 int a[N],b[N],c[N]; 19 20 int check(int x){ 21 for(int i=1;i<=n;i++){ 22 if(a[i]<x)b[i]=b[i-1]-1; 23 else b[i]=b[i-1]+1; 24 c[i]=min(c[i-1],b[i]); 25 } 26 int f=0; 27 for(int i=k;i<=n;i++){ 28 if(b[i]-c[i-k]>0){ 29 f=1; 30 break; 31 } 32 } 33 return f; 34 } 35 int main(){ 36 IO; 37 cin>>n>>k; 38 for(int i=1;i<=n;i++){ 39 cin>>a[i]; 40 } 41 int l=1,r=n+1; 42 while(r-l>1){ 43 int m=(l+r)/2; 44 if(check(m))l=m; 45 else r=m; 46 } 47 cout<<l<<endl; 48 }