其他分享
首页 > 其他分享> > CodeForces - 645 C.Enduring Exodus

CodeForces - 645 C.Enduring Exodus

作者:互联网

快乐二分

用前缀和随便搞一下

#include <cstdio>
using namespace std;
const int N = 100010;
int p[N];
int n, k, cnt = 0;
inline int msum(int a, int b) {
    if (a < 0)
        a = 0;
    return (b - a - sum[b] + sum[a]);
}

inline bool judge(int s) {
    for (int i = 1; i <= n; i++) {
        if (p[i] == 1)
            continue;
        if (msum(i - s - 1, i - 1) + msum(i, i + s) >= k)
            return true;
    }
    return false;
}

int main() {
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
        sum[i] = sum[i - 1] + p[i];
    }
    int l = 0, r = n / 2 + 1;
    while (l < r) {
        int mid = (l + r) / 2;
        if (judge(mid))
            r = mid;
        else
            l = mid + 1;
    }
    printf("%d\n", l);
    return 0;
}

 

标签:cnt,return,int,sum,CodeForces,645,inline,Exodus,false
来源: https://www.cnblogs.com/cminus/p/11963904.html