滑动窗口问题

最长含有不小于x的子数组

cs 复制代码
#include <stdio.h>

int main() {
    int n;
    long long x, k;  // x 和 k 可能很大,用 long long
    scanf("%d %lld %lld", &n, &x, &k);

    long long a[100005];  // 根据数据规模 n <= 10^5
    for (int i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
    }

    int left = 0;
    int count = 0;  // 当前窗口内 >= x 的元素个数
    int max_len = -1;

    for (int right = 0; right < n; right++) {
        // 扩展右边界
        if (a[right] >= x) {
            count++;
        }

        // 如果当前窗口内有效元素个数超过了 k,收缩左边界
        while (count > k) {
            if (a[left] >= x) {
                count--;
            }
            left++;
        }

        // 如果当前窗口内有效元素个数正好是 k,更新最大长度
        if (count == k) {
            int current_len = right - left + 1;
            if (current_len > max_len) {
                max_len = current_len;
            }
        }
    }

    printf("%d\n", max_len);
    return 0;
}
相关推荐
一只齐刘海的猫11 小时前
【Leetcode】找到字符串中所有字母异位词
算法·leetcode·职场和发展
海清河晏11112 小时前
数据结构 | 八大排序
数据结构·算法·排序算法
liulilittle12 小时前
固定数组时间轮的槽过载优化:桶链表与批次执行
网络·数据结构·链表
IronMurphy13 小时前
【算法五十七】146. LRU 缓存
算法·缓存
Irissgwe13 小时前
数据结构-栈和队列
数据结构·c++·c·栈和队列
两片空白13 小时前
数据容器集合set/frozenset
数据结构
凌波粒13 小时前
LeetCode--108.将有序数组转换为二叉搜索树(二叉树)
算法·leetcode·职场和发展
liulilittle13 小时前
KCC:在 BBR 思路上的一次探索
网络·tcp/ip·算法·bbr·通信·拥塞控制·kcc
浦信仿真大讲堂14 小时前
达索系统SIMULIA Abaqus 2026接触和约束的增强新功能介绍
人工智能·python·算法·仿真软件·达索软件