Assignment

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

Total Submission(s): 627    Accepted Submission(s): 318

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

解题思路:

枚举左端点。二分查找右端点,用RMQ维护区间最大值和最小值

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <stack>
#define LL long long
using namespace std;
const int MAXN = 100000 + 10;
int A[MAXN];
int dp1[MAXN][20], dp[MAXN][20];
int N, M;
void RMQ_init_Min()
{
for(int i=0;i<N;i++) dp1[i][0] = A[i];
for(int j=1;(1<<j) <= N;j++)
{
for(int i=0;i+(1<<j) - 1 < N;i++)
{
dp1[i][j] = min(dp1[i][j-1], dp1[i + (1<<(j-1))][j-1]);
}
}
}
int RMQ_Min(int L, int R)
{
int k = 0;
while(1<<(k+1) <= R - L + 1) k++;
return min(dp1[L][k], dp1[R-(1<<k)+1][k]);
}
void RMQ_init_Max()
{
for(int i=0;i<N;i++) dp[i][0] = A[i];
for(int j=1;(1<<j) <= N;j++)
{
for(int i=0;i+(1<<j) - 1 < N;i++)
{
dp[i][j] = max(dp[i][j-1], dp[i + (1<<(j-1))][j-1]);
}
}
}
int RMQ_Max(int L, int R)
{
int k = 0;
while(1<<(k+1) <= R - L + 1) k++;
return max(dp[L][k], dp[R-(1<<k)+1][k]);
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &M);
for(int i=0;i<N;i++) scanf("%d", &A[i]);
RMQ_init_Min();
RMQ_init_Max();
long long ans = 0;
for(int i=0;i<N;i++)
{
int l = i, r = N-1;
while(l <= r)
{
int mid = (l + r) >> 1;
int low = RMQ_Min(i, mid);
int high = RMQ_Max(i ,mid);
//cout << l << ' ' << r << ' ' << high << ' ' <<low << endl;
if(high - low < M) l = mid + 1;
else r = mid - 1;
}
//cout << l << endl;
ans += (l - i);
}
printf("%I64d\n", ans);
}
return 0;
}

HDU 5289 Assignment(2015 多校第一场二分 + RMQ)的更多相关文章

  1. HDU 5288 OO&#39;s sequence (2015多校第一场 二分查找)

    OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  2. hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...

  3. HDU 5289 Assignment(多校联合第一场1002)

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

  4. HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...

  5. hdu 5294 Tricks Device(2015多校第一场第7题)最大流+最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294   题意:给你n个墓室,m条路径,一个人在1号墓室(起点),另一个人在n号墓室(终点),起点的那 ...

  6. hdu 5288 OO’s Sequence(2015多校第一场第1题)枚举因子

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 题意:在闭区间[l,r]内有一个数a[i],a[i]不能整除 除去自身以外的其他的数,f(l,r ...

  7. hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语

    题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=44 题意:给你n个n,如果能在n-1次运算之后(加减乘除)结果为24的输出n-1次运算的过程,如果不能输 ...

  8. hdu 5301 Buildings (2015多校第二场第2题) 简单模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题意:给你一个n*m的矩形,可以分成n*m个1*1的小矩形,再给你一个坐标(x,y),表示黑格子 ...

  9. HDU6581 Vacation (HDU2019多校第一场1004)

    HDU6581 Vacation (HDU2019多校第一场1004) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6581 题意: 给你n+1辆汽车, ...

随机推荐

  1. compass和sass很好的两篇文章

    Sass是一种"CSS预处理器",可以让CSS的开发变得简单和可维护.但是,只有搭配Compass,它才能显出真正的威力. 本文介绍Compass的用法.毫不夸张地说,学会了Com ...

  2. 自己定制Linux发行版(资料)

    /******************************************************************** * 自己定制Linux发行版(资料) * 声明: * 随着L ...

  3. c#中单元测试

    从走进.net后发现每天有写不完的代码,有做不完的测试...人感觉都已经机械,我们需要认清自己调整好心态,问下自己是否真的喜欢编程.我的答案当然也就是我爱编码,编码给我带来了许多欢乐,每天都给我体验小 ...

  4. 使用VSS2005的时候报错:输入正确的服务器地址依然出错了

    使用VSS2005的时候报错:输入正确的服务器地址依然出错了 使用VSS2005的时候报错: 在安装完vss客户端,进入vss服务器的时候,需要vss服务器的ip和数据库名称.以及初始化文件, 我在进 ...

  5. 静态Web开发 HTML

    静态Web开发 一章 HTML(Hyper Text Markup Language) 1节html入门 HTML超文本标记语言由浏览器解释执行开发人员编写的超文本文档就是网页 XHTMLHTML升级 ...

  6. HDU 1561-The more, The Better(树状背包)

    题意: n个城堡,每个有一定的财富,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡,求应攻克哪m个城堡,获得最大财富. 分析:dp[i][j],以i为根的子树,攻克j个城堡,获得的 ...

  7. LR 常见问题总结

    问题1:Error: Two Way Communication Error:            Function two_way_comm_post_message/two_way_comm_p ...

  8. 移动端混合型App(hybrid app)自动化测试选型与实践

    背景 公司产品的业务已经发展到了移动端,开发选型已经结束,决定使用phonegap做移动端的web应用开发平台.考虑到业务的复杂与多样,移动端的测试同样需要自动化.在网上看了很多,最终锁定了3个移动端 ...

  9. OpenGL 加载位图(BMP)贴图

    GLuint Texture[1]; HBITMAP hBMP; BITMAP BMP; glGenTextures(1, &Texture[0]); hBMP = (HBITMAP)Load ...

  10. Git管理命令

     1.创建分支 git branch <分支名> 2.切换分支 git checkout <分支名> 创建并切换到该分支:git checkout -b <分支名> ...