HDU 5806 · NanoApe Loves Sequence Ⅱ【尺取法】

【题意】

退休狗NanoApe,他准备重新参加国家高等教育入学考试!在数学科目上,他再次拾起数列(这个知识)。他在纸上写下了一个满足数字 n m(条件)的序列。现在他想知道在序列中第k大的数不小于 m 的连续子序列的数的数目。
注意:子序列的长度必须不小于k
限制条件:
1T10
2n2105
1kn2
1m,Ai109

关键原文:
Now he wants to know the number of continous subsequences of the sequence in such a manner that the kth largest number in the subsequence is no less than m .

【提炼】

给定一个数列,以及m,k值,求满足区间里第k大的数不小于m的区间数值的个数(即区间长)。

【分析】

因为要求出第k大的数满足某个条件,第一反应是分治法来处理;提到必须连续,进而想到尺取法,我们只要处理出大于等于m的区间前缀和,然后累加区间长就可以了。

具体做法是这样:
我们把大于等于m的数记为1,累加至k个,即满足所求条件,累加区间长即可。

Tips:由于要累加很多的区间长,这个值会爆int型,建议使用范围更大的long long型。(坑点)

【时间复杂度】

显然,

O(n)

【代码】

/*
    coder:  Tangent Chang
    date:   2017/5/14
    A day is a miniature of eternity. by Emerson
*/


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>

using namespace std;

typedef long long ll;

int d[200000 + 5];

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

    while (T--) {
        int n, k, m;
        scanf("%d%d%d", &n, &m, &k);
        for (int i = 0; i < n; ++i) {
            scanf("%d", &d[i]);
        }
        int s = 0, t = 0, cnt = 0;
        ll res = 0;
        while (s < n) {
            while (cnt < k && t < n) {
                cnt += (d[t++] >= m);
            }
            if (cnt == k) {
                res += n - t + 1;
            }
            cnt -= (d[s++] >= m);
         }
         printf("%lld\n", res);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值