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;
}