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
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]
题意:在序列 A 中 有多少个区间 满足 最大值 - 最小值 < k
分析一:
直接找满足条件的区间 , 但是我们不好枚举区间 , 分析问题: 会发现 能选择的区间 是被 一个个特殊的值给隔开了。既从左到右 会依次出现 ....X1....X2....X3...X4.. 。 这些 值会造成 Xi - Xj>=k(i>j) 。而且这些值肯定为 某个区间的最值。
解法就是:从左到右用单调队列维护最大最小值,出现MAx -MIN>k
分析二:注意到固定l之后(要学会分析问题啊!!),随着r的右移,[l,r][l,r]的最大值越来越大,[l,r][l,r]的最小值越来越小,所以 MAX - MIN 是单调递增的。二分答案就行。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#define fr first
#define se second
#define Pa pair<int,int>
const int maxn = 200000;
const int inf = 1e9;
typedef long long ll;
using namespace std;
int n,dif;
int h[maxn+5];
int qmax[maxn+5],qmin[maxn+5];
void solve()
{
ll ans = 0;
int l,r,fr1=1,fr2=1,se1=0,se2=0;
for(l=1,r=1;r<=n;r++)
{
while(fr1<=se1&&qmax[se1]<h[r]) se1--;
qmax[++se1]=h[r];
while(fr2<=se2&&qmin[se2]>h[r]) se2--;
qmin[++se2]=h[r];
while(fr1<=se1&&fr2<=se2&&qmax[fr1]-qmin[fr2]>=dif)
{
ans+=r-l;
if(qmax[fr1]==h[l]) fr1++;
if(qmin[fr2]==h[l]) fr2++;
l++;
}
}
for(;l<=n;l++) ans+=n-l+1;
printf("%I64d\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&dif);
for(int i=1;i<=n;i++) scanf("%d",&h[i]);
solve();
}
return 0;
}
二分:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#define fr first
#define se second
#define Pa pair<int,int>
const int maxn = 200000;
const int inf = 1e9;
typedef long long ll;
using namespace std;
int n,dif;
int h[maxn+5];
ll ans;
pair<int,int>d[maxn+5][20];
void Init()
{
for(int i=1;i<=n;i++) d[i][0].fr = h[i], d[i][0].se = h[i];
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;(1<<j)+i-1<=n;i++)
{
d[i][j].fr = max(d[i][j-1].fr,d[i+(1<<(j-1))][j-1].fr);
d[i][j].se = min(d[i][j-1].se,d[i+(1<<(j-1))][j-1].se);
}
}
}
Pa RMQ(int L,int R)
{
int k = 0;
while((1<<(k+1)<=R-L+1)) k++;
Pa p;
p.fr = max(d[L][k].fr,d[R-(1<<k)+1][k].fr);
p.se = min(d[L][k].se,d[R-(1<<k)+1][k].se);
return p;
}
void solve()
{
ans = 0;
for(int i=1;i<=n;i++)
{
int l=i,r=n+1;
while(r-l>1)
{
int mid = (l+r)>>1;
Pa p = RMQ(i,mid);
int Max = p.fr, Min=p.se;
if(Max-Min<dif) l = mid;
else r = mid;
}
ans+=(l-i+1)*1ll;
}
printf("%lld\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&dif);
for(int i=1;i<=n;i++) scanf("%d",&h[i]);
Init();
solve();
}
return 0;
}