博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
B - Assignment
阅读量:5139 次
发布时间:2019-06-13

本文共 3212 字,大约阅读时间需要 10 分钟。

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,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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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--)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<
<=n;j++) { for(int i=1;i+(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<
<=r) x++; x--; return max(dpmax[l][x],dpmax[r-(1<
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
#include
#include
#include
#include
#include
#include
#include
#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
#include
#include
#define rep(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
v1;deque
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]) 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<
<

转载于:https://www.cnblogs.com/wzl19981116/p/9577889.html

你可能感兴趣的文章
深浅拷贝(十四)
查看>>
由级别和性格特征将程序员分类 ---看看你属于哪一种
查看>>
HDU 6370(并查集)
查看>>
BZOJ 1207(dp)
查看>>
PE知识复习之PE的导入表
查看>>
HDU 2076 夹角有多大(题目已修改,注意读题)
查看>>
洛谷P3676 小清新数据结构题(动态点分治)
查看>>
九校联考-DL24凉心模拟Day2T1 锻造(forging)
查看>>
Cortex M3/M4 学习摘要(二)
查看>>
C#时间的味道——任时光匆匆我只在乎你
查看>>
(1)数据结构——线性表(数组)实现
查看>>
SpringMyBatis解析2-SqlSessionFactoryBean
查看>>
按照excel文档中的内容在当前cad图纸中自动排布实体
查看>>
Winform开发框架之图表报表在线设计器2-图表-SNF.EasyQuery项目--SNF快速开发平台3.3-Spring.Net.Framework...
查看>>
C#基础第八天-作业-设计类-面向对象方式实现两个帐户之间转账
查看>>
洛谷 P3237 [HNOI2014]米特运输
查看>>
Attributes.Add用途与用法
查看>>
JavaScript面向对象初探——封装和继承
查看>>
L2-001 紧急救援 (dijkstra+dfs回溯路径)
查看>>
【概率】poj 2096:Collecting Bugs
查看>>