HDU 5289 Assignment(单调队列)

Assignment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 297    Accepted Submission(s): 152


Problem 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
Hint
First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
 

 

Source
 
 
题意:有N个人要分组,每个组的人的编号要连续而且每组任意两人的差值要小于k,求满足条件的分组有多少个。
分析:用单调队列维护最值。
可以用Fi来表示以第i个点结尾的满足条件的区间的个数,结果ans=sum{Fi}。
区间[i,j]中的区间个数=j-i+1;
那么就要维护一个最大值和最小值的队列来计算出不符合条件的区间,然后减去不符合条件的区间。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=100000+5;
int a[MAXN],q1[MAXN],q2[MAXN];
int kase,n,k;
int main()
{
    cin>>kase;
    while(kase--)
    {
        LL ans=0;
        int f1,f2,t1,t2,l1,l2;
        f1=f2=t1=t2=0;
        l1=l2=-1;
        scanf("%d %d",&n,&k);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
        {
            while(f1<t1 && a[q1[t1-1]]<=a[i]) t1--;//维护最大值队列(递减)
            q1[t1++]=i;
            while(f2<t2 && a[q2[t2-1]]>=a[i]) t2--;//维护最小值队列(递增)
            q2[t2++]=i;
            while(a[q1[f1]] - a[q2[f2]]>=k)//差值过大
                q1[f1]<q2[f2] ? l1=q1[f1++] : l2=q2[f2++];
            ans+=i-max(l1,l2);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

posted @ 2015-07-21 22:00  Cliff Chen  阅读(202)  评论(0编辑  收藏  举报