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

First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]

问有多少个团体,团体要求能力最大和最小的差距不超过k,rmq,因为区间越长,最大值越大最小值越小,所以可以用二分找到左端点固定,右端点最右的区间,就可以了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=1e5+10;
int a[N];
int dpmax[N][20],dpmin[N][20];
void first(int n)
{
mm(dpmax,0);
mm(dpmin,0);
rep(i,1,n+1)
{
dpmin[i][0]=dpmax[i][0]=a[i];
}
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)
{
dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
}
}
}
int fmax(int l,int r)
{
int x=0;
while(l-1+(1<<x)<=r) x++;
x--;
return max(dpmax[l][x],dpmax[r-(1<<x)+1][x]);
}
int fmin(int l,int r)
{
int x=0;
while(l-1+(1<<x)<=r) x++;
x--;
return min(dpmin[l][x],dpmin[r-(1<<x)+1][x]);
}
bool judge(int l,int mid,int k)
{
return fmax(l,mid)-fmin(l,mid)<k;
}
int main()
{
int re;scf(re);
while(re--)
{ int n,k;scff(n,k);
rep(i,1,n+1) scf(a[i]);
first(n);
ll ans=0;
rep(i,1,n)
{
int l=i,r=n,mid;
while(r-l>1)
{
mid=(l+r)/2;
if(judge(i,mid,k))
l=mid;
else
r=mid;
}
while(judge(i,l,k)&&l<=n)
l++;
l--;
ans+=l-i+1;
}
ans++;
pf("%lld\n",ans);
} }

还有一种用deque的方法 ,速度更快,慢慢体会吧...

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<deque>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=1e5+10;
int a[N];
deque<int>v1;
deque<int>v2;
int main()
{
int re,n,k;
scf(re);
while(re--)
{
ll ans=0;
scff(n,k);
rep(i,1,n+1)
scf(a[i]);
v1.clear();
v2.clear();
int i,j;
for( i=1,j=1;i<=n;i++)
{
while(!v1.empty()&&v1.back()<a[i]) v1.pop_back();
v1.push_back(a[i]);
while(!v2.empty()&&v2.back()>a[i]) v2.pop_back();
v2.push_back(a[i]);
while(!v1.empty() &&!v2.empty() &&(v1.front()-v2.front()>=k))
{
ans+=i-j;
if(v1.front()==a[j]) v1.pop_front();
if(v2.front()==a[j]) v2.pop_front();
j++;
}
}
while(j<=n)
{
ans+=i-j;
j++;
}
cout<<ans<<endl;
}
return 0;
}

B - Assignment的更多相关文章

  1. Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为“通用职责分配软件模式”

    Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为"通用职责分配软件模式" 1. GRA ...

  2. user initialization list vs constructor assignment

    [本文连接] http://www.cnblogs.com/hellogiser/p/user_initialization_list.html [分析] 初始化列表和构造函数内的赋值语句有何区别? ...

  3. Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removing it

    Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removi ...

  4. 代写assignment

    集英服务社,强于形,慧于心 集英服务社,是一家致力于优质学业设计的服务机构,为大家提供优质原创的学业解决方案.多年来,为海内外学子提供了多份原创优质的学业设计解决方案. 集英服务社,代写essay/a ...

  5. [Top-Down Approach] Assignment 1: WebServer [Python]

    Today I complete Socket Programming Assignment 1 Web Server Here is the code: #!/usr/bin/python2.7 # ...

  6. default constructor,copy constructor,copy assignment

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  7. Programming Assignment 5: Kd-Trees

    用2d-tree数据结构实现在2维矩形区域内的高效的range search 和 nearest neighbor search.2d-tree有许多的应用,在天体分类.计算机动画.神经网络加速.数据 ...

  8. Programming Assignment 4: 8 Puzzle

    The Problem. 求解8数码问题.用最少的移动次数能使8数码还原. Best-first search.使用A*算法来解决,我们定义一个Seach Node,它是当前搜索局面的一种状态,记录了 ...

  9. Programming Assignment 2: Randomized Queues and Deques

    实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...

  10. 逆转序列的递归/尾递归(+destructuring assignment)实现(JavaScript + ES6)

    这里是用 JavaScript 做的逆转序列(数组/字符串)的递归/尾递归实现.另外还尝鲜用了一下 ES6 的destructuring assignment + spread operator 做了 ...

随机推荐

  1. linux下修改/dev/shm tmpfs文件系统大小

    默认系统就会加载/dev/shm ,它就是所谓的tmpfs,有人说跟ramdisk(虚拟磁盘),但不一样.象虚拟磁盘一样,tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储.而且传统的 ...

  2. Android: Android Studio签名打包的两种方式(zz)

    注:给我们自己开发的app签名,就代表着我自己的版权,以后要进行升级,也必须要使用相同的签名才行.签名就代表着自己的身份(即keystore),多个app可以使用同一个签名. 如果不知道签名是啥意思, ...

  3. Spark2.3(四十二):Spark Streaming和Spark Structured Streaming更新broadcast总结(二)

    本次此时是在SPARK2,3 structured streaming下测试,不过这种方案,在spark2.2 structured streaming下应该也可行(请自行测试).以下是我测试结果: ...

  4. centos exfat格式U盘不支持问题

    centos exfat格式U盘不支持问题 1. 下载fuse-exfat-1.3.0-1.el7.x86_64.rpm 2. 终端安装 rpm  -ivh fuse-exfat-1.3.0-1.el ...

  5. 【转】关于免费SSL证书的那些事儿

    根据 Let’s Encrypt CA 的统计,截至 2017 年 11 月,Firefox 加载的网页中启用 HTTPS 的比例占 67%,比去年底的 45% 有巨大提升.浏览器开发商如 Mozil ...

  6. Swift实时画箭头的实现

    iOS上实现画箭头,如果是指定了坐标点,那是很简单的,但如果需要做到实时绘制,就需要计算一下了 需求: 在白板上,根据手势落下点和移动点,实时绘制一条箭头直线(如下图) 实现代码: /// 获取箭头的 ...

  7. MATLAB 统计元素出现的次数

    可以使用 hist 函数: A = [1 2 8 8 1 8 2 1 8 2 1]; count = hist(A,unique(A)) count的结果与unique(A)对应.

  8. Linux(CentOS)安装Node.JS和npm的两种方式(yum安装和源码安装)

    yum安装 yum安装是将yum源中的rpm包下载到本地,安装这个rpm包.这个rpm包是别人编译安装好的二进制包.这种方式方便快捷,特别是不用考虑包依赖. 0.了解linux版本 通过 uname ...

  9. HDU2082母函数模板题

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  10. Data Model for Message Receiver

    1. Physical Data Model 2. SQL Statements drop database MessageReceiver go /*======================== ...