当前位置:网站首页>hdu5289(Assignment)
hdu5289(Assignment)
2022-07-27 09:54:00 【51CTO】
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
Sample Output
题意:给定一个正整数序列,问有多少个连续子序列,其中最大值与最小值之差小于k?
题解:神奇的单调队列,用一个单调递增和一个单调递减队列来维护最小值与最大值,用两个前后指针,前指针指向当前区间左端点,后指针指向当前元素。如果当前加入新元素之后,max-min>=k,那么与新元素相差至少k的那个元素之前的所有元素都将失效,也就是出队。每一次操作答案都加上新元素向左可以延伸的最大距离。
例子:n=6,k=3,序列为5 3 5 2 3 4。q1为单调增队列,q2为单调减队列。
这时候发现q1中最小值与q2中最大值差值3>=k,那么开始出队。
所以最后答案为12
边栏推荐
- Understand chisel language. 26. Chisel advanced input signal processing (II) -- majority voter filtering, function abstraction and asynchronous reset
- 食品安全 | 无糖是真的没有糖吗?这些真相要知道
- 视觉SLAM十四讲笔记(一):第一讲+第二讲
- Final examination paper of engineering materials
- 去 OPPO 面试,被问麻了
- Oracle RAC 19C PDB instance is down
- 华为交换机双上行组网Smart-link配置指南
- ACL2021最佳论文出炉,来自字节跳动
- Towards the peak of life
- 会议OA项目之会议排座功能&&会议送审的实现
猜你喜欢

视觉SLAM十四讲笔记(一):第一讲+第二讲

食品安全 | 垃圾食品越吃越想吃?这份常见食品热量表请收好

Leetcode.814. binary tree pruning____ DFS

历时一年,论文终于被国际顶会接收了

How to restore the original version after installing Hal Library

超赞的卡尔曼滤波详解文章

吃透Chisel语言.25.Chisel进阶之输入信号处理(一)——异步输入与去抖动
![[SCM]源码管理 - perforce 分支的锁定](/img/c6/daead474a64a9a3c86dd140c097be0.jpg)
[SCM]源码管理 - perforce 分支的锁定

交换机端口镜像配置指南

How does data analysis solve business problems? Here is a super detailed introduction
随机推荐
面试必备:虾皮服务端15连问
Towards the peak of life
Annotation and reflection
Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
Exercises --- quick arrangement, merging, floating point number dichotomy
视觉SLAM十四讲笔记(一):第一讲+第二讲
华为交换机双上行组网Smart-link配置指南
I grabbed a ticket and thought I found the system bug of 12306
Understand chisel language. 26. Chisel advanced input signal processing (II) -- majority voter filtering, function abstraction and asynchronous reset
达梦 PARTGROUPDEF是自定义的对象吗?
吃透Chisel语言.27.Chisel进阶之有限状态机(一)——基本有限状态机(Moore机)
oracle rac 19c pdb实例当掉
WGAN、WGAN-GP、BigGAN
Cannot start after installing MySQL 5.7.27 in CentOS 7? (Language bash)
Overview of PCL modules (1.6)
Switch port mirroring Configuration Guide
如何在树莓派上安装cpolar内网穿透
Shell的read 读取控制台输入、read的使用
圆环工件毛刺(凸起)缺口(凹陷)检测案例
Snowflake vs. databricks who is better? The latest war report in 2022