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. 我的app自动化实战练习一

    ''' -*- coding: utf-8 -*- @Time : 2019/6/10 0010 10:39 @Author : 无邪 @File : test_data.py @Software: ...

  2. django模板系统的基本原则

    写模板,创建template对象,创建 context ,调用render()方法

  3. Chrome安装助手踩坑

    [前言] 最近用之前的方法配置hosts,想浏览下载国外网站的数据和插件,突然发现几乎所有的方法都无效了...... 本文介绍下下载谷歌助手,通过助手访问国外网站 [主体] (1)搜索谷歌助手,点击下 ...

  4. JavaSE-31 Java正则表达式

    概述 正则表达式是一个强大的字符串处理工具,可以实现对字符串的查找.提取.分割.替换等操作. String类的几个方法需要依赖正则表达式的支持. 方法 方法说明 boolean matches(Str ...

  5. OpenCV2:第一章 图像表示

    一.简介 在OpenCV中,可以用C++语法的Mat类来表示一张图像 也可以用C语法的lpllmage或CvMat结构体来表示一张图像 1.单通道像素值 2.多通道像素值 OpenCV默认颜色顺序为B ...

  6. OpenCV2:第四章 导出图像

    一.简介 一般我们用OpenCV来处理图像数据的时候,OpenCV已经把图像数据包装成一个图像数据类,我们只需要对类成员的像素值进行修改就行了. 但是在Windows开发的WinSDK/MFC中,对图 ...

  7. java内存模型(线程独占部分)

    线程独占部分 1.你了解Java的内存模型吗? 内存简介 有内核空间.用户空间(java是运行在用户空间上) 32位系统--->最大的访问内存大小是4G 62位系统--->最大的访问内存大 ...

  8. java文件上传,自动判断文件类型

    public enum FileType { /** * JEPG. */ JPEG("FFD8FF"), /** * PNG. */ PNG("89504E47&quo ...

  9. NOI2018_Day1_T1_归程

    题目描述 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 n 个节点.m 条边的无向连通图(节点的编号从 1 至 n). 我们依次用 l,a 描述一条边的长度. ...

  10. 用python的Requests库模拟http请求

    一.先了解几个重要的http请求头或响应头信息 Request Headers: Host: 描述请求将被发送的目的地,包括,且仅仅包括域名和端口号. Origin: 说明请求从哪里发起的,包括,且仅 ...