Assignment

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5289

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

题意

给你n个数,然后连续的,且这个区间内最大值减去最小值的差小于k的话,就可以算作一队。

问你一共有多少种分队的方法。

题解:

暴力枚举左端点位置,然后二分枚举右端点,用一个rmq维护一下就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
struct RMQ{
const static int RMQ_size = maxn;
int n;
int ArrayMax[RMQ_size][21];
int ArrayMin[RMQ_size][21]; void build_rmq(){
for(int j = 1 ; (1<<j) <= n ; ++ j)
for(int i = 0 ; i + (1<<j) - 1 < n ; ++ i){
ArrayMax[i][j]=max(ArrayMax[i][j-1],ArrayMax[i+(1<<(j-1))][j-1]);
ArrayMin[i][j]=min(ArrayMin[i][j-1],ArrayMin[i+(1<<(j-1))][j-1]);
}
} int QueryMax(int L,int R){
int k = 0;
while( (1<<(k+1)) <= R-L+1) k ++ ;
return max(ArrayMax[L][k],ArrayMax[R-(1<<k)+1][k]);
} int QueryMin(int L,int R){
int k = 0;
while( (1<<(k+1)) <= R-L+1) k ++ ;
return min(ArrayMin[L][k],ArrayMin[R-(1<<k)+1][k]);
} void init(int * a,int sz){
n = sz ;
for(int i = 0 ; i < n ; ++ i) ArrayMax[i][0] = ArrayMin[i][0] = a[i];
build_rmq();
} }solver;
int a[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,k;scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
solver.init(a,n);
long long ans = 0;
for(int i=0;i<n;i++)
{
int l = i,r = n-1,Ans=i;
while(l<=r)
{
int mid = (l+r)/2;
if(solver.QueryMax(i,mid)-solver.QueryMin(i,mid)<k)l=mid+1,Ans=mid;
else r=mid-1;
}
ans += (Ans-i+1);
}
cout<<ans<<endl;
}
return 0;
}

最新文章

  1. 你所能用到的BMP格式介绍
  2. Android XML中引用自定义内部类view的四个why
  3. Windows自带压缩解压工具
  4. docker中使用systemd
  5. SQL语句汇总(终篇)—— 表联接与联接查询
  6. sql 数据库换行
  7. '用Roslynpad做一个轻量级的C#编辑器'
  8. php入门变量
  9. 首页banner焦点图自动轮播效果
  10. extjs 框架模板
  11. iOS 错误 之 Potential leak of an object stored into &#39;cs&#39;
  12. Material使用03 MdCardModule模块、MdInputModule模块
  13. [BZOJ1610] [Usaco2008 Feb] Line连线游戏 (set)
  14. 5、Storm集成Kafka
  15. Flask关于请求表单的粗浅应用及理解+简单SQL语句温习
  16. str的用法
  17. Mac下TensorFlow安装及环境搭建
  18. Python学习笔记(四):字符串的学习
  19. “Hello World!”团队——Final发布用户使用报告
  20. 洛谷P2147 [SDOI2008] 洞穴勘探 [LCT]

热门文章

  1. mysql之安装和配置(一)
  2. python基础===中文手册,可查询各个模块
  3. 深入理解python多进程编程
  4. centos7 安装rlwrap
  5. cordova热更新插件的使用
  6. JS内存管理与垃圾回收
  7. 【我要学python】愣头青之小数点精度控制
  8. Windows 下使用 mingw+msys 交叉编译 Android Unity Mono
  9. Flask实战第54天:cms删除轮播图功能完成
  10. Knockout.js(二):监控数组属性(Observables Arrays)