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. C# 框架是什么?MVC是什么 ?工厂模式是什么?设计模式是什么?三层架构是什

    C# 框架是什么?MVC是什么 ?工厂模式是什么?设计模式是什么?三层架构是什么?如果要学我该从何学起??? C# 框架看这里http://download.csdn.net/source/25784 ...

  2. 1019.Line Painting(线段树 离散化)

    1019 离散化都忘记怎么写了 注意两个端点 离散化后用线段树更新区间 混色为-1  黑为2  白为1  因为N不大 最后直接循环标记这一段的颜色查找 #include <iostream> ...

  3. poj 2531 Network Saboteur( dfs )

    题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...

  4. sencha touch2 动画问题

    最近在review一个项目的代码, 发现返回操作比较乱,很多"从哪里来,到哪里去的操作"被写的一塌糊涂; 按照ios系统的进场出场动画(人家的体验还是很好的,必须借鉴)为标准,使用 ...

  5. Spark(1) - Getting Started with Apache Spark

    Introduction Apache Spark is a general-purpose cluster computing system to process big data workload ...

  6. 正则化(Regularization)

    正则化(Regularization)是机器学习中抑制过拟合问题的常用算法,常用的正则化方法是在损失函数(Cost Function)中添加一个系数的\(l1 - norm\)或\(l2 - norm ...

  7. 【转】Parallels Desktop 11.2.0 破解版 最佳Mac虚拟机软件

    原文网址:http://www.macappstore.net/parallels-desktop-11-pojie-ban/ Parallels Desktop 11.2.0 破解版 最佳Mac虚拟 ...

  8. 【转】Android项目中编译 C的模块

    原文网址:http://blog.csdn.net/Harrison_zhu/article/details/4057738 Android编译环境本身比较复杂,且不像普通的编译环境:只有顶层目录下才 ...

  9. iOS开发中提交带有中文或特殊字符串的参数

    iOS开发中,与后台进行数据交换是一个很常见的场景. 在web开发中,对于我们提交的地址,浏览器会负责进行decode,但是在ios中,必须要自己手动来实现.否则我们拼接出的网址在包括中文.特殊字符串 ...

  10. How to cancel parallel loops in .NET C# z

    Cancellation token Parallel options CancellationTokenSource cancellationTokenSource = new Cancellati ...