hdu 5289 - Assignment(2015 Multi-University Training Contest 1 )单调队列+RMQ+树状数组

Assignment

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<=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],…,an,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]

Author
FZUACM

Source
2015 Multi-University Training Contest 1

思路:用单调队列维护最大值最小值,按顺序不断向队尾添加数据,当最大值最小值的差大于等于k后,就意味着新添加的这个不能作用于当前第二个指针的位置,也就能计算出,以第二个指针位置开始的连续子序列的个数,最后统计总和。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int INF=1e9+7;
int N,K;
int a[maxn],q1[maxn],q2[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&K);
        for(int i=1;i<=N;i++)scanf("%d",&a[i]);
        LL ans=0;
        int f1=0,r1=0,f2=0,r2=0;
        q1[0]=q2[0]=0;
        for(int i=1;i<=N;i++)
        {
            while(f1<=r1&&a[q1[r1]]<=a[i])r1--;
            q1[++r1]=i;
            while(f2<=r2&&a[q2[r2]]>=a[i])r2--;
            q2[++r2]=i;
            while(a[q1[f1]]-a[q2[f2]]>=K)
            {
                if(q1[f1]<q2[f2])f1++;
                else f2++;
            }
            if(f2<=0)ans+=i-q1[f1-1];
            else ans+=i-max(q1[f1-1],q2[f2-1]);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

RMQ跟树状数组是一样的,二分,然后得到区间最大最小值,看是不是小于K

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int INF=1000000010;
int N,K;
int Max,Min;
int a[maxn];
int A[maxn][20],B[maxn][20];
void process()
{
    for(int i=1;i<=N;i++)
        A[i][0]=B[i][0]=a[i];
    for(int j=1;(1<<j)<=N;j++)
        for(int i=1;i+(1<<j)<=N+1;i++)
            A[i][j]=max(A[i][j-1],A[i+(1<<(j-1))][j-1]),
            B[i][j]=min(B[i][j-1],B[i+(1<<(j-1))][j-1]);

}
void query(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))k++;
    Max=max(A[l][k],A[r-(1<<k)+1][k]);
    Min=min(B[l][k],B[r-(1<<k)+1][k]);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&K);
        for(int i=1;i<=N;i++)scanf("%d",&a[i]);
        process();
        LL ans=0;
        for(int i=1;i<=N;i++)
        {
            int l=i,r=N;
            while(l<r)
            {
                int mid=l+(r-l+1)/2;
                Max=0,Min=INF;
                query(i,mid);
                if(Max-Min>=K)r=mid-1;
                else l=mid;
            }
            ans+=l-i+1;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int INF=0x3f3f3f3f;
int N,K;
int a[maxn];
int Max,Min;
int tree1[maxn],tree2[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int val,bool flag)
{
    while(x<=N)
    {
        if(flag)tree1[x]=max(tree1[x],val);
        else tree2[x]=min(tree2[x],val);
        x+=lowbit(x);
    }
}
void query(int x,bool flag)
{
    while(x>0)
    {
        if(flag)Max=max(Max,tree1[x]);
        else Min=min(Min,tree2[x]);
        x-=lowbit(x);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&K);
        for(int i=1;i<=N;i++)scanf("%d",&a[i]);
        memset(tree1,0,sizeof(tree1));
        memset(tree2,INF,sizeof(tree2));
        LL ans=0;
        for(int i=N;i>0;i--)
        {
            add(i,a[i],0);
            add(i,a[i],1);
            int l=i,r=N;
            while(l<r)
            {
                int mid=l+(r-l+1)/2;
                Min=INF,Max=0;
                query(mid,0);
                query(mid,1);
                if(Max-Min>=K)r=mid-1;
                else l=mid;
            }
            ans+=l-i+1;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值