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. 暑假集训 || 树DP

    树上DP通常用到dfs https://www.cnblogs.com/mhpp/p/6628548.html POJ 2342 相邻两点不能同时被选 经典题 f[0][u]表示不选u的情况数,此时v ...

  2. JavaSE-02 变量 数据类型和运算符

    学习要点 掌握变量的概念 掌握常用数据类型 掌握赋值运算符.算术运算符 掌握boolean数据类型和关系运算符 掌握变量的概念 面向过程程序的定义 程序的定义:程序=数据+算法+文档 程序要操作的数据 ...

  3. OpenCV2:第九章 图像比较

    一.简介 图像相似度主要是对两幅图像内容的相似程度进行打分,根据分数的高低来判断图像内容的相似程度. 常见的图像比较有两种方法:峰值信噪比PSNR和结构相似性SSIM 二.峰值信噪比PSNR(Peak ...

  4. [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)

    题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...

  5. PyQt5-Python3-PyCharm 配置

    File->Tools->External Tools->add 配置ui文件转换工具 Name: PyUIC Program: D:\develop\python\Mac\venv ...

  6. 【JDBC】Servlet实例

    import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.Dri ...

  7. 树莓派 - MQTT

    安装mosquitto 下载源代码包 wget http://mosquitto.org/files/source/mosquitto-1.5.tar.gz 解压 tar zxfv mosquitto ...

  8. JavaIO基础学习笔记

    JavaIO JavaIO即Java的输入输出系统.比如我们的程序要读取一个文本文件.一张图片或者要获取控制台输入的内容,就要用到输入流:又或者程序要将生成的一段字符窜以文件的形式保存到系统中就要用到 ...

  9. 3.3.4 使用 awk 重新编排字段

    awk 本身所提供的功能完备,已经是一个很好用的程序语言了.以后会好好地介绍该语言的精髓.虽然 awk 能做的事很多,但它主要的设计是要在 Shell脚本中发挥所长:做一些简单的文本处理,例如取出字段 ...

  10. unittest多线程生成报告(BeautifulReport)

    前言 selenium多线程跑用例,这个前面一篇已经解决了,如何生成一个测试报告这个是难点,刚好在github上有个大神分享了BeautifulReport,完美的结合起来,就能生成报告了. 环境必备 ...