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. NPOI的源代码编译

    打开版本库下的examples文件夹 然后打开对应的解决方案文件,尝试编译程序.发现提示缺少了dll 琢磨了半天,找到四个项目文件,打开之后进行编译.最后会生成dll到solution文件夹下的Lib ...

  2. MAC 上搭建lua环境

    一.下载并安装 (1)最新release版下载地址 http://www.lua.org/ftp/lua-5.3.1.tar.gz (2)编译 Building Lua is implemented ...

  3. poj1286Necklace of Beads(ploya定理)

    链接 这个东东是新知识 let's 从头学起吧 这篇文库讲的不错 至少把各种概念学了一遍 然后再看此题 共有两种类型的置换 一种是旋转之后相同算一种 一种是翻转之后相同算一种 对于旋转 共有N次置换 ...

  4. 使用Amoeba 实现MySQL DB 读写分离

    Amoeba(变形虫)项目是一个开源框架,于2008年开始发布一款 Amoeba for Mysql软件: 这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当SQ ...

  5. 设计模式Day02

    1.生成器模式 生成器模式也称为建造者模式.生成器模式的意图在于将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示. 生成器模式的编程步骤: (1)定义一个产品类:  由于不在该类完 ...

  6. ClassLoader工作机制

    阅读目录 一.ClassLoader概念 二.JVM平台提供三层classLoader 三.JVM加载class文件到内存有两种方式 四.ClassLoader加载类的过程 五.自定义类加载器 六.实 ...

  7. 如何将域中的AD数据导入SharePoint

    转:http://www.cnblogs.com/wallis0922/archive/2010/09/29/1838292.html 最近刚装好sharepoint2010,想要研究一下,第一件想做 ...

  8. .NET 4.0中的泛型协变和逆变

    随Visual Studio 2010 CTP亮相的C#4和VB10,虽然在支持语言新特性方面走了相当不一样的两条路:C#着重增加后期绑定和与动态语言相容的若干特性,VB10着重简化语言和提高抽象能力 ...

  9. 2014 多校联合训练赛6 Fighting the Landlords

    本场比赛的三个水题之一,题意是两个玩家每人都持有一手牌,问第一个玩家是否有一种出牌方法使得在第一回和对方无牌可出.直接模拟即可,注意一次出完的情况,一开始没主意,wa了一发. #include< ...

  10. 浏览器的CSS Hacks

    LZ注:此文原作者是:Paul Irish(Google的前端开发工程师),本文是原文的部分译文. 我不再使用CSS Hacks了,相反的是,我将使用IE的条件判断将类应用到body标签.   但是, ...