UVA 1451 - Average

膜拜刘汝佳大神的思路和代码!

#include <cstdio>
#include <iostream>
using namespace std;

const int maxn = 100000 + 5;

int n, L;
char s[maxn];
int sum[maxn], p[maxn]; ///sum-前缀和,p-里面存的是可能作为起点的点的坐标

int compare_average(int x1, int x2, int x3, int x4) {  ///  斜率
    return (sum[x2]-sum[x1-1]) * (x4-x3+1) - (sum[x4]-sum[x3-1]) * (x2-x1+1);
}

int main() {
    int T;
    scanf("%d", &T);

    while(T--) {
        scanf("%d%d%s", &n, &L, s+1);

        sum[0] = 0;
        for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + s[i] - '0';

        int ansL = 1, ansR = L;

        int i = 0, j = 0;
        for (int t = L; t <= n; t++) {
            while (j-i > 1 && compare_average(p[j-2], t-L, p[j-1], t-L) >= 0) j--;///删掉下凸点
            p[j++] = t-L+1;

            while (j-i > 1 && compare_average(p[i], t, p[i+1], t) <= 0) i++;

            int c = compare_average(p[i], t, ansL, ansR);
            if (c > 0 || c == 0 && t - p[i] < ansR - ansL) {
                ansL = p[i];
                ansR = t;
            }
        }
        printf("%d %d\n", ansL, ansR);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值