R1 A median of Niuke NOIP improvement group (dichotomy)

Keywords: C++ less

meaning of the title

Title Link

Sol

It's a fairy's question. There will only be $n^2 $violence in the exam room..

Consider bisecting a $mid $directly, and let's see if the final answer is likely to be greater than $x $.

When judging, just record the minimum prefix value,

Let $s[i] $indicate how much of $1-i $is larger than it. The required length is $len $. We note that the minimum value of $s[i - len] $is $Mi$

If $s [i] - Mi > 0 $, then in the interval with a length of at least $len $, the number greater than $mid $and the number less than $mid $are still larger than $mid $after offsetting each other. At this time, $mid $is legal

 

For the first time, this kind of dichotomy is done, but the answer is not the number given. Fairy qwq

/*
 
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#include<set>
#include<vector>
//#define int long long
#define LL long long
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = 1e5 + 10, INF = 1e9 + 10, mod = 1e9 + 7;
const double eps = 1e-9;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, len, s[MAXN], a[MAXN];
int check(int x) {
    for(int i = 1; i <= N; i++)
        if(a[i] < x) s[i] = -1; 
        else s[i] = 1;//s[i] :  1 - i What's the ratio in x Big 
    int Mi = 0;
    for(int i = 1; i <= N; i++) {
        s[i] += s[i - 1];
        if(i >= len) Mi = min(Mi, s[i - len]);
        if(i >= len && (s[i] - Mi > 0)) return 1;
    }
    return 0;
} 
int main() {
//    freopen("a.in", "r", stdin);
//    freopen("c.out", "w", stdout);
    N = read(); len = read();
    for(int i = 1; i <= N; i++) a[i] = read();
    int l = 0, r = 1e9 + 10, ans;
    while(l <= r) {
        int mid = l + r >> 1;
        if(check(mid)) ans = mid, l = mid + 1;//Is there a ratio? mid Big solution 
        else r = mid - 1;
    }
    printf("%d", ans);
    return 0;
}
/*
5 4
7 2 3 2 6 
*/

Posted by bouwob on Tue, 31 Dec 2019 23:10:05 -0800