思路:
二分答案
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pii pair<int, int> #define piii pair<pii, int> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e5 + 10; int a[N], b[N], n, len; bool check(int x) { for (int i = 1; i <= n; i++) if(a[i] < x) b[i] = -1; else b[i] = 1; int mn = INT_MAX; for (int i = 1; i <= n; i++) { b[i] += b[i-1]; if(i >= len) mn = min(mn, b[i-len]); if(i >= len && b[i] - mn > 0) return true; } return false; } int main() { scanf("%d %d", &n, &len); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); LL l = 1, r = 1e9, m = l+r+1 >> 1; while(l < r) { if(check(m)) l = m; else r = m-1; m = l+r+1 >> 1; } printf("%d ", m); return 0; }