sliding window

本文介绍了一种计算数组中滑动窗口内的最大值和最小值的方法,通过使用双端队列,可以在O(n)时间内完成计算。

Description

An array of size  n ≤ 10 6 is given to you. There is a sliding window of size  k which is moving from the very left of the array to the very right. You can only see the  k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is  [1 3 -1 -3 5 3 6 7], and  k is 3.
Window positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

Your task is to determine the maximum and minimum values in the sliding window at each position. 

Input

The input consists of two lines. The first line contains two integers  n and  k which are the lengths of the array and the sliding window. There are  n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source


# include <stdio.h>
# define M 2100000
int n,i,k,a[M],Q[M],I[M];

void getMin()
{
    int head=1,tail=0;
    for(i=1;i<k;i++)
    {
        while(head<=tail&&Q[tail]>a[i])    tail--;
        tail++;
        Q[tail]=a[i];
        I[tail]=i;
    }
    for(i=k;i<=n;i++)
    {
        while(head<=tail&&Q[tail]>=a[i])    tail--;
        tail++;
        Q[tail]=a[i];
        I[tail]=i;
        while(I[head]<=i-k)   head++;
        printf("%d ",Q[head]);
    }
}

void getMax()
{
    int head=1,tail=0;
    for(i=1;i<k;i++)
    {
        while(head<=tail&&Q[tail]<=a[i])    tail--;
        tail++;
        Q[tail]=a[i];
        I[tail]=i;
    }
    for(i=k;i<=n;i++)
    {
        while(head<=tail&&Q[tail]<=a[i])    tail--;
        tail++;
        Q[tail]=a[i];
        I[tail]=i;
        while(I[head]<=i-k)   head++;
        printf("%d ",Q[head]);
    }
}

int main()
{
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++)    scanf("%d",&a[i]);
    getMin();
    printf("\n");
    getMax();
    return 0;
}
### 关于滑动窗口在数据增强中的应用 滑动窗口(Sliding Window)作为一种高效的技术,在许多领域得到了广泛应用,特别是在处理连续序列或固定长度的子数组时。尽管滑动窗口的主要用途在于优化算法性能[^4],但它也可以被扩展应用于数据增强场景。 #### 滑动窗口在数据增强中的作用 在机器学习和深度学习中,数据增强是一种常用的方法,用于扩充训练集并提升模型泛化能力。对于时间序列或文本数据,滑动窗口可以通过截取不同片段的方式生成新的样本。这种方式不仅保留了原始数据的时间顺序特性,还能够在一定程度上模拟真实世界中的噪声或变化。 具体来说,滑动窗口的数据增强方法可以从以下几个方面实现: 1. **随机采样**:通过设定不同的窗口大小和步长,从原始数据集中提取多个子序列作为新样本。 2. **局部扰动**:在滑动窗口的基础上引入一定的随机性,比如对窗口内的某些值进行轻微修改或替换。 3. **多尺度分析**:利用不同大小的滑动窗口捕获全局特征和局部细节,从而构建多层次的输入表示。 以下是基于 Python 的简单代码示例展示如何使用滑动窗口来进行数据增强操作: ```python import numpy as np def sliding_window_augmentation(data, window_size=50, step_size=10, noise_level=0.01): """ 使用滑动窗口技术进行数据增强 参数: data (list or array): 输入的一维时间序列数据 window_size (int): 窗口大小 step_size (int): 步幅大小 noise_level (float): 添加噪音的比例 返回: list of arrays: 增强后的数据列表 """ augmented_data = [] for i in range(0, len(data) - window_size + 1, step_size): window = data[i:i+window_size].copy() # 可选:向窗口内加入少量高斯白噪 if noise_level > 0: noise = np.random.normal(loc=0, scale=noise_level * np.std(window), size=len(window)) window += noise augmented_data.append(window) return augmented_data # 测试用例 time_series = np.sin(np.linspace(0, 10, num=500)) # 创建一个简单的正弦波信号 augmented_samples = sliding_window_augmentation(time_series, window_size=50, step_size=10) print(f"Original Data Length: {len(time_series)}") print(f"Number of Augmented Samples: {len(augmented_samples)}") ``` 上述代码展示了如何通过滑动窗口机制从单条时间序列中抽取若干短片段,并可选项地为其添加一些人工制造的小幅度波动以进一步丰富数据多样性[^3]。 #### 影响与优势 采用这种策略的好处包括但不限于以下几点: - 提升模型鲁棒性; - 减少过拟合风险; - 更好地捕捉序列模式下的细微差异。 然而需要注意的是,当选择较大的 `step_size` 或较小的 `window_size` 时可能会丢失部分重要信息,因此参数调优至关重要。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值