数据结构·窗口滑动

在这里插入图片描述

学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。

学习日记

An array of size n ≤ 106 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 position Minimum value Maximum value
[1 3 -1] -3 5 3 6 7 -1 3
1 [3 -1 -3] 5 3 6 7 -3 3
1 3 [-1 -3 5] 3 6 7 -3 5
1 3 -1 [-3 5 3] 6 7 -3 5
1 3 -1 -3 [5 3 6] 7 3 6
1 3 -1 -3 5 [3 6 7] 3 7

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

输入格式:

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

输出格式:

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.

输入样例:

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

输出样例:

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

代码长度限制                                                                                                16 KB

时间限制                                                                                                        10000 ms

内存限制                                                                                                        62 MB

参考代码:

#include <stdio.h>
/*
void bubblesort(int a[],int n)
{
        int i,t;
    for(i=0; i<n-1; i++)
    {
        for(int j=0; j<n-1; j++)
            if(a[j]<a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
    }
}

int main()
{
    //nst s=106;
    int n,k,a[106];   //a[s]大数组
    int b[106];       //b[s]可移动数组
    int time=0,count1=0,count2=0;   //time表示小数组一共移动的次数
                                    //count1
    // void comper(int a;int b;int c)
    //void bubble_sort(int a[],int n);
    
    
    scanf("%d %d",&n,&k);   //确定n和k,的值
    
    for(int i=0;i<n;i++)    //输入大数组数据
    {
        scanf("%d",&a[i]);
    }
    
    time=n-(k-1); //time计算公式
    
    for(int j=0;j<time;j++) //大轮回,小框移动
    {
        for(int x=0;x<k;x++)    //框内循环
        {
            b[x]=a[count1];     //把值导入新数组
            count1++;           //改变count1值
            
            for(int p=0;p<k;p++)    //框内循环
            {
                bubblesort(b,k);   //排序
                if(p<k-1)
                {
                    printf("%d ",b[0]);
                }
                else
                {
                    printf("%d",b[0]);
                }
            }*/
            /*
            if(count1<time)
            {
                printf("%d ",b[0]);
            }
            else
            {
                printf("%d",b[0]);
            }
            *//*
        }*/
        /*
        for(int x=0;x<k;x++)
        {
            b[x]=a[count2];
            count2++;
            
            for(int m=0;m<k;m++)    //框内循环
            {
                bubblesort(b,k);   //排序
                if(m<k-1)
                {
                    printf("%d ",b[k-1]);
                }
                else
                {
                    printf("%d",b[k-1]);
                }*/
                /*
            if(count2<time)
            {
                printf("%d ",b[k-1]);
            }
            else
            {
                printf("%d",b[k-1]);
            }
            *//*
        }
    }*/
    
    /*
    return 0;
}
}*/
/*
void comper(int a;int b;int c)
{
    int a, b, c, max, min;
    
    scanf("%d%d%d", &a, &b, &c);
    
    max = a>b ? a:b;
    max = c>max ? c:max;
    
    min = a<b ? a:b;
    min = c< min ? c:min;
    
    //printf("max=%d", max);
    //printf("min=%d", min);
}
*/
    /*
void bubble_sort(int a[],int n) //冒泡排序
                                //int a[] 要排序的数组
                                //int n 数组元素的个数
{
    for(int i=0; i<n-1; i++)
    {
        bool isSorted = true;
        for(int j=0; j<n-1-i; j++)
        {
            if(a[j] > a[j+1])
            {
                isSorted = false;
                int temp = a[j];
                a[j] = a[j+1];
                a[j+1]=temp;
            }
        }
        if(isSorted)
        {
            break;
        }
    }
}
*/
int minnumber[1000000],maxnumber[1000000];//小窗口的最小值和最大值
int a[1000000],b[1000000]; //最小值和最大值
int number[1000000];

int main()
{
    int minf=0,mint=0,maxf=0,maxt=0; //最小值队列的队首、尾&最大值队列的队首、尾
    int n,k,i=1;
    
   scanf("%d%d",&n,&k);
    
    for(i=1;i<=n;i++)
    {
       scanf("%d",&number[i]);
    }
    
    for(i=1;i<=n;i++)//数组中有值且队尾值≥数组元素,尾元素出,使首元素是最小值,维持队列递
    {
       while(minf < mint && number[a[mint-1]] >= number[i])
       {
           mint--;
       }
     a[mint]=i;
     mint++;
      //数组中有值且尾元素值≤数组元素,尾元素出,使队首元素是最大值,维持队列递减
        while(maxf < maxt && number[b[maxt-1]] <= number[i])
        {
            maxt--;
        }
        b[maxt]=i;
        maxt++;
        
        if(i>=k)
        {
            while(i-a[minf]>=k)
            {
                minf++;
            }
            minnumber[i-k+1] = number[a[minf]];
            
            while(i-b[maxf]>=k)
            {
                maxf++;
            }
            maxnumber[i-k+1] = number[b[maxf]];
        }
    }
   
    for(i=1;i<= n-k+1;i++)
    {
        printf("%d ",minnumber[i]);
    }
    printf("\n");
    
    for(i=1;i<=n-k+1;i++)
    {
        printf("%d ",maxnumber[i]);
    }
    
   return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_63794226/article/details/127468383