hdu5289(Assignment)

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 n, k (1<=n<=100000, 0<k<=10^9),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]<=10^9),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


题意:给定一个正整数序列,问有多少个连续子序列,其中最大值与最小值之差小于k?


题解:神奇的单调队列,用一个单调递增和一个单调递减队列来维护最小值与最大值,用两个前后指针,前指针指向当前区间左端点,后指针指向当前元素。如果当前加入新元素之后,max-min>=k,那么与新元素相差至少k的那个元素之前的所有元素都将失效,也就是出队。每一次操作答案都加上新元素向左可以延伸的最大距离。

例子:n=6,k=3,序列为5 3 5 2 3 4。q1为单调增队列,q2为单调减队列。

step1 : 

q1 : 5  

q2 : 5

l = 0, r = 0

ans = 1

step2 : 

q1 : 3

q2 : 5 3

l = 0, r = 1

ans = 1 + 2

step3 : 

q1 : 3 5

q2 : 5

l = 0, r = 2

ans = 1 + 2 + 3

step4 : 

q1 : 2

q2 : 5 2

l = 0, r = 3

这时候发现q1中最小值与q2中最大值差值3>=k,那么开始出队。

q1 : 2

q2 : 2

l = 3, r = 3

ans = 1 + 2 + 3 + 1

step5 : 

q1 : 2 3

q2 : 3

l = 3, r = 4

ans = 1 + 2 + 3 + 1 + 2

step6 : 

q1 : 2 3 4

q2 : 4

l = 3, r = 5

ans = 1 + 2 + 3 + 1 + 2 + 3

所以最后答案为12


#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;

int a[100010];
deque<int> q1, q2;


int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, k;
        cin >> n >> k;
        for (int i = 0; i < n; ++i)
            scanf("%d", &a[i]);
        q1.clear();
        q2.clear();
        int l = 0, r = 0;
        long long ans = 0;
        while (r < n)
        {
            while (!q1.empty() && q1.back() > a[r])
                q1.pop_back();
            q1.push_back(a[r]);
            while (!q2.empty() && q2.back() < a[r])
                q2.pop_back();
            q2.push_back(a[r]);
            while (!q1.empty() && !q2.empty() && q2.front() - q1.front() >= k)
            {
                if (q1.front() == a[l])
                    q1.pop_front();
                if (q2.front() == a[l])
                    q2.pop_front();
                l++;
            }
            ans += r - l + 1;
            r++;
        }
        cout << ans << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法码上来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值