Assignment ZQOJ 25691 单调队列

Assignment

  • Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
  • Total Submission(s): 17 Accepted Submission(s): 10
Description

Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.

Input

In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers nk (1 ≤ n ≤ 100000, 0 < k ≤ 109), indicate the company has n persons, k means the maximum difference between abilities of staff in a group is less than k. The second line contains n integers: a[1], a[2], …, a[n] (0 ≤ a[i] ≤ 109), indicate the i-th staff's ability.

Output

For each test,output the number of groups.

Sample Input

2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9

Sample Output

5
28

Hint

First Sample, the satisfied groups include: [1,1] , [2,2] , [3,3] , [4,4] , [2,3]

Source
2015 Multi-University Training 1



单调队列介绍:

我们从最简单的问题开始:

给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k.

要求:

f(i) = max{a(i-k+1),a(i-k+2),..., a(i)},i = 0,1,...,N-1

问题的另一种描述就是用一个长度为k的窗在整数数列上移动,求窗里面所包含的数的最大值。

解法一:

很直观的一种解法,那就是从数列的开头,将窗放上去,然后找到这最开始的k个数的最大值,然后窗最后移一个单元,继续找到k个数中的最大值。

这种方法每求一个f(i),都要进行k-1次的比较,复杂度为O(N*k)。

那么有没有更快一点的算法呢?

解法二:

我们知道,上一种算法有一个地方是重复比较了,就是在找当前的f(i)的时候,i的前面k-1个数其它在算f(i-1)的时候我们就比较过了。那么我们能不能保存上一次的结果呢?当然主要是i的前k-1个数中的最大值了。答案是可以,这就要用到单调递减队列。

单调递减队列是这么一个队列,它的头元素一直是队列当中的最大值,而且队列中的值是按照递减的顺序排列的。我们可以从队列的末尾插入一个元素,可以从队列的两端删除元素。

1.首先看插入元素:为了保证队列的递减性,我们在插入元素v的时候,要将队尾的元素和v比较,如果队尾的元素不大于v,则删除队尾的元素,然后继续将新的队尾的元素与v比较,直到队尾的元素大于v,这个时候我们才将v插入到队尾。

2.队尾的删除刚刚已经说了,那么队首的元素什么时候删除呢?由于我们只需要保存i的前k-1个元素中的最大值,所以当队首的元素的索引或下标小于i-k+1的时候,就说明队首的元素对于求f(i)已经没有意义了,因为它已经不在窗里面了。所以当index[队首元素]<i-k+1时,将队首元素删除。

从上面的介绍当中,我们知道,单调队列与队列唯一的不同就在于它不仅要保存元素的值,而且要保存元素的索引(当然在实际应用中我们可以只需要保存索引,而通过索引间接找到当前索引的值)。

为了让读者更明白一点,我举个简单的例子。

假设数列为:8,7,12,5,16,9,17,2,4,6.N=10,k=3.

那么我们构造一个长度为3的单调递减队列:

首先,那8和它的索引0放入队列中,我们用(8,0)表示,每一步插入元素时队列中的元素如下:

0:插入8,队列为:(8,0)

1:插入7,队列为:(8,0),(7,1)

2:插入12,队列为:(12,2)

3:插入5,队列为:(12,2),(5,3)

4:插入16,队列为:(16,4)

5:插入9,队列为:(16,4),(9,5)

。。。。依此类推

那么f(i)就是第i步时队列当中的首元素:8,8,12,12,16,16,。。。

题目代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef struct Node{int val,pos;} node ;
deque < node> q1,q2;//双向队列
int main()
{
    int a[100005];
    int t,n,k;
    scanf("%d",&t);
    while(t--)
    {
        long long  ans=0;
        int head=0;
        scanf("%d %d",&n,&k);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        while(!q1.empty())
            q1.pop_back();
        while(!q2.empty())
            q2.pop_back();
        for(int i=0;i<n;i++)
        {
            node tmp;
            tmp.val=a[i];
            tmp.pos=i;
            while(!q1.empty())
            {
                node tmp5=q1.back();
                if(tmp.val>tmp5.val)
                    q1.pop_back();
                else
                    break;
            }
            q1.push_back(tmp);
            while(!q2.empty())
            {
                node tmp6=q2.back();
                if(tmp.val<tmp6.val)
                   q2.pop_back();
                else
                    break;
            }
            q2.push_back(tmp);
            if(i==0)
                ans++;
            else
            {
                while(1)
                {
                    node big=q1.front();
                    node small=q2.front();
                    printf("%d %d\n",big.val,small.val);
                    if(big.val-small.val<k)  //当前窗口的最值满足要求
                        break;
                    else
                    {
                        printf("..\n");
                        if(big.pos>small.pos)  //当前窗口不满足条件,那么就将窗口前移1位,并且队首出栈。
                        {
                            head=small.pos+1;
                            q2.pop_front();
                        }
                        else
                        {
                            head=big.pos+1;
                            q1.pop_front();
                        }
                    }
                }
                ans+=i-head+1;
            }
        }
        printf("%lld\n",ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值