传送门: hdu5289
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]
解题:数组在a[]的子集中,问有多少个子集中满足差值小于k?最简单的思路就是逐一枚举进行判断。
例如,对于子集[a,x](a是常数),对于x的取值范围二分判断区间最值是否小于k,进而确定x范围。具体看代码吧
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1e5+10;
int n,k,a[maxn],dmax[maxn][20],dmin[maxn][20];//ST算法
void RMQ(){for(int i=1;i<=n;i++){dmax[i][0]=a[i];dmin[i][0]=a[i];}for(int j=1;(1<<j)<=n;j++){for(int i=1;i+(1<<j)-1<=n;i++){dmax[i][j]=max(dmax[i][j-1],dmax[i+(1<<(j-1))][j-1]);dmin[i][j]=min(dmin[i][j-1],dmin[i+(1<<(j-1))][j-1]);}}
}int query(int x,int y){int k=(int)(log(y-x+1.0)/log(2.0));return max(dmax[x][k],dmax[y-(1<<k)+1][k])-min(dmin[x][k],dmin[y-(1<<k)+1][k]);
}long long solve(){long long ans=0;int cnt=0;//二分判断x for(int i=1;i<=n;i++){int lb=i,ub=n;while(ub-lb>1){int mid=(ub+lb)>>1;if(query(i,ub)<k) cnt++;if(query(i,mid)>=k)ub=mid;elselb=mid;}//cout<<cnt<<"**"<<endl;//cout<<"AAA"<<endl;//x的范围为[i,lb]或[i,ub] if(query(i,ub)<k){ //判断ub是否满足 ans+=ub-i+1;}else{ans+=lb-i+1;}}return ans;
}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]);}RMQ();long long ans=solve();printf("%lld\n",ans);}return 0;
}
贪心,另一种二分写法
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1e5+10;
int n,k,a[maxn],dmax[maxn][20],dmin[maxn][20];//ST算法
void RMQ(){for(int i=1;i<=n;i++){dmax[i][0]=a[i];dmin[i][0]=a[i];}for(int j=1;(1<<j)<=n;j++){for(int i=1;i+(1<<j)-1<=n;i++){dmax[i][j]=max(dmax[i][j-1],dmax[i+(1<<(j-1))][j-1]);dmin[i][j]=min(dmin[i][j-1],dmin[i+(1<<(j-1))][j-1]);}}
}int query(int x,int y){int k=(int)(log(y-x+1.0)/log(2.0));return max(dmax[x][k],dmax[y-(1<<k)+1][k])-min(dmin[x][k],dmin[y-(1<<k)+1][k]);
}long long solve(){long long ans=0;//二分判断x ,另一种写法 for(int i=1;i<=n;i++){int lb=i,ub=n;while(ub>=lb){//cout<<"AAA"<<endl;int mid=(ub+lb)>>1;if(query(i,mid)>=k)ub=mid-1;elselb=mid+1;}ans+=ub-i+1;}return ans;
}/*
long long solve(){ //贪心选取右端点 long long ans=0;int l=1;for(int i=1;i<=n;i++){while(l<i&&query(l,i)>=k) l++;ans+=i-l+1;}return ans;
} */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]);}RMQ();long long ans=solve();printf("%lld\n",ans);}return 0;
}