Assignment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 557    Accepted Submission(s): 280

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]
 
Author
FZUACM
 
Source
 
Recommend
We have carefully selected several similar problems for you:  5299 5298 5297 5296 5295 

rmq+二分

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define eps 1e-8
typedef __int64 ll;
const int mod=1e9+7; using namespace std; #define N 100007 int a[N],n;
int dpmin[N][25],dpmax[N][25];
int k; inline bool judge(int le,int ri)
{
int kk=log2((ri-le+1)*1.0);
int mi=min(dpmin[le][kk],dpmin[ri-(1<<kk)+1][kk]);
int ma=max(dpmax[le][kk],dpmax[ri-(1<<kk)+1][kk]);
return ma-mi<k;
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
dpmin[i][0]=dpmax[i][0]=a[i]; for(j=1;(1<<j)<=n;j++)
for(i=1;i+(1<<j)-1<=n;i++)
{
int p=1<<(j-1);
dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+p][j-1]);
dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+p][j-1]);
} __int64 ans=0;
int le,ri,p;
for(i=1;i<=n;i++)
{
le=i;
ri=n;
while(le<=ri)
{
int mid=(le+ri)>>1;
if(judge(i,mid))
{
p=mid;
le=mid+1;
}
else
ri=mid-1;
}
ans+=p-i+1;
}
printf("%I64d\n",ans);
}
return 0;
}

单调队列

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define eps 1e-8 using namespace std;
const int mod=1e9+7; #define INF 0x3f3f3f3f const int N=100005; int mique[N],maque[N],mihead,mahead,mitail,matail;
int n,a[N],k; int pre,now;
__int64 ans; inline void miinque(int i)
{
while(mihead<mitail&&a[i]<a[mique[mitail-1]]) mitail--;
mique[mitail++]=i;
} inline void mainque(int i)
{
while(mahead<matail&&a[i]>a[maque[matail-1]]) matail--;
maque[matail++]=i;
} void outque(int pos)
{
if(a[maque[mahead]]-a[mique[mihead]]>=k)
{
int nowlen=pos-now-1;
int prelen=pre-now;
ans+=(__int64)(nowlen+1)*nowlen/2;
if(prelen>=1)
ans-=(__int64)(prelen+1)*prelen/2;
pre=pos-1;
}
while(a[maque[mahead]]-a[mique[mihead]]>=k)
if(mique[mihead]<maque[mahead])
{
now=mique[mihead]+1;
mihead++;
}
else
{
now=maque[mahead]+1;
mahead++;
}
} int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
mihead=mahead=mitail=matail=0;
pre=now=1;
ans=0;
for(i=1;i<=n;i++)
{
miinque(i);
mainque(i);
outque(i);
}
if(pre<n)
{
int nowlen=n-now;
int prelen=pre-now;
ans+=(__int64)(nowlen+1)*(nowlen)/2;
if(prelen>=1)
ans-=(__int64)(prelen+1)*(prelen)/2;
pre=n-1;
} printf("%I64d\n",ans+n);
}
return 0;
}

HDU 5089 Assignment(rmq+二分 或 单调队列)的更多相关文章

  1. HDU - 5289 Assignment (RMQ+二分)(单调队列)

    题目链接: Assignment  题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...

  2. BZOJ_3316_JC loves Mkk_ 二分答案 + 单调队列

    BZOJ_3316_JC loves Mkk_ 二分答案 + 单调队列 题意: 分析: 拆成链,二分答案,奇偶两个单调队列维护最大子段和,记录方案. 代码: #include <cstdio&g ...

  3. hdu 3706 Second My Problem First 单调队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...

  4. [NOIP2017普及组]跳房子(二分,单调队列优化dp)

    [NOIP2017普及组]跳房子 题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 nn 个格子, ...

  5. 感染(low)bfs 、感染(mid) 二分、感染(high) 二分 + 维护单调 队列去除无用的点

    感染(low) Description n户人家住在一条直线上,从左往右依次编号为1,2,-,n.起初,有m户人家感染了COVID-19,而接下来的每天感染的人家都会感染他家左右两家的人,问t天后总共 ...

  6. HDU 4122 Alice's mooncake shop 单调队列优化dp

    Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  7. codeforces 251A Points on Line(二分or单调队列)

    Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...

  8. hdu 4122 Alice's mooncake shop(单调队列)

    题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...

  9. hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)

    题目链接:hdu 3415 Max Sum of Max-K-sub-sequence 题意: 给你一串形成环的数,让你找一段长度不大于k的子段使得和最大. 题解: 我们先把头和尾拼起来,令前i个数的 ...

随机推荐

  1. docker 创建容器与管理容器

    创建容器的选项 [root@mast ~]# docker container run --help Usage: docker container run [OPTIONS] IMAGE [COMM ...

  2. eclipse生成spring boot jar包

    1.右击项目,选择Run As - Maven clean 2.右击项目,选择Run As - Maven install 3.成功后 会在项目的target文件夹下生成jar包 4.将打包好的jar ...

  3. Chrome浏览器安装React developer tools

    1. 到 https://github.com/facebook/react-devtools 下载 react-devtools 2. 进入 react-devtools 目录 运行命令  npm ...

  4. Maven实战读书笔记(三):Maven依赖

    3.1 依赖的配置 一个依赖声明可以包含下面元素: <dependencies> <dependency> <groupId></groupId> &l ...

  5. php微信公众号开发之快递查询

    [文章来源:脚本之家   文章地址:https://www.jb51.net/article/149205.htm] 本文实例为大家分享了php微信公众号开发之快递查询的具体代码,供大家参考,具体内容 ...

  6. java中list或数组中随机子集工具类

    package com.example.demo.test; import java.util.ArrayList;import java.util.Arrays;import java.util.L ...

  7. Oracle的五种约束

    1.非空(NOT NULL)约束:所定义的列不绝对不能为空: 例如:将已经创建好的表BOOK中的bookname字段修改为不为空: 利用 ALTER TABLE.......MODIFY ...... ...

  8. validate 常用的输入框校验

    记录一下angular可以直接用的输入框校验器,外加一个国内手机号码的校验 <!DOCTYPE html> <html> <head> <meta chars ...

  9. Python之阻塞IO模型与非阻塞IO模型

    Python之阻塞IO模型与非阻塞IO模型 IO模型 1 阻塞IO: 全程阻塞 2 非阻塞IO: 发送多次系统调用: 优点:wait for data时无阻塞 缺点:1 系统调用太多 2 数据不是实时 ...

  10. gitlab+jenkins+docker自动构建

    docker容器部署gitlab: sudo docker run --detach \ --hostname git.gitlab.com \ --net=host \ --publish 9443 ...