zoukankan      html  css  js  c++  java
  • 16级第三周寒假作业F题

    Sliding Window

    TimeLimit:12000MS  MemoryLimit:65536K
    64-bit integer IO format:%lld
    Problem Description

    Case Time Limit: 5000MS

    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 positionMinimum valueMaximum 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.

    本题数据极其巨大,可能要进行输出优化,否则会超时,具体代码可以参考下面

    void Out(int a)    //输出一个整型
    {
        if(a<0)
        {
              putchar('-');
              a=-a;
        }
        if(a>9)
        Out(a/10);
        putchar(a%10+'0');
    }

    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.

    SampleInput
    8 3
    1 3 -1 -3 5 3 6 7
    SampleOutput
    -1 -3 -3 -3 3 3
    3 3 5 5 6 7

    思路:首先这道题很坑,很坑,很坑;然后进入主题,我是用的单调队列,对于初始状态,我们可以加入k-1个元素,然后每次加一个元素,进行弹出和查询的操作。因为要处理最大值和最小值,所以我们就创建两条队列,
    分别用来处理最大值和最小值,这里只需要说明一下最小值的队列就可以了,最大值队列同理。
    假如当前我们要加入一个新的元素x,我们只需要将x与队尾的元素进行比较,如果我们发现x小于队尾的元素,那么显然队尾的那个元素是没有用的,可以直接删除。这个原理就是对于当前加入的x,接下来的所有询问中,有
    队员元素在的询问一定有x在,也就是说无论如何队尾元素都不会是最小值,我们可以直接删除。
    我们就可以发现这个优先队列的性质就是前面的数字总是小于后面的数字,所以查询的时候,最小的数字就是队首的元素
    接下来还有一个删除的操作,这个更加简单,我们只要判断队首的元素与当前加入的这个数的位置,看看差值是不是大于或等于k就可以决定这个数是不是需要删除了。
    然后就是我的输入输出的优化,因为数据较大,优化一下免得超时;
    具体的看我的代码;
     1 int read(int &x)
     2 {
     3     char c=getchar();
     4     int f=1;x=0;
     5     while(c<'0'||c>'9')
     6     {
     7         if(c=='-')
     8             f=-f;
     9         c=getchar();
    10     }
    11     while(c>='0'&&c<='9')
    12     {
    13         x=x*10+c-'0';
    14         c=getchar();
    15     }
    16     x*=f;
    17 }
    
    
    void Out(int a)    //输出一个整型
    {
        if(a<0)
        {
              putchar('-');
              a=-a;
        }
        if(a>9)
        Out(a/10);
        putchar(a%10+'0');
    }
     1 for(int i=1;i<=n;i++)
     2     {
     3         read(a[i]);
     4         while(a[q1[T1]]>=a[i]&&T1>=H1)
     5             T1--;
     6         while (a[q2[T2]]<=a[i]&&T2>=H2)
     7             T2--;
     8         q1[++T1]=q2[++T2]=i;
     9         if(q1[H1]+k<=i)
    10             H1++;
    11         if (q2[H2]+k<=i)
    12             H2++;
    13         ans1[i]=a[q1[H1]];
    14         ans2[i]=a[q2[H2]];
    15     }




  • 相关阅读:
    阿里云CentOS安装firefox闪退
    error: QApplication: No such file or directory
    CentOS下自动登陆root帐户
    Linux下常用软件
    【记录】haphost免费vps初始配置
    检测Linux VPS是Xen、OpenVZ还是KVM真假方法
    使用VNC远程管理VPS(Centos系统)
    配置suricata
    NGUI的输入框制作(attach- input filed script的使用)
    NGUI技能CD效果制作(sprite的type:filled)
  • 原文地址:https://www.cnblogs.com/tijie/p/6358849.html
Copyright © 2011-2022 走看看