Assignment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3813    Accepted Submission(s): 1771

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]

题目链接:HDU 5289

题意就是求有多少个连续子串,这些子串均符合相邻数之间差的绝对值均小于k,直接两个for计数估计会T,因此可以枚举子串的左端点$i$,二分右端点$R$,使得$[i,R]$长度最大,那么这样一来这个子串是肯定符合的,实际上把右端点往左缩一个得到的小一个单位的子串也肯定是符合的,这样可以一直缩到区间变成$[i,i]$,因此每一次枚举得到的区间$[i,R]$可以产生$R-i+1$个符合题意的子串。

另外由于最大答案可以达到$\frac{(1+10^5)*10^5} {2}$因此要用long long

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e5 + 7;
int arr[N], Max[N][20], Min[N][20]; void rmq_init(int l, int r)
{
int i, j;
for (i = l; i <= r; ++i)
Max[i][0] = Min[i][0] = arr[i];
for (j = 1; l + (1 << j) - 1 <= r; ++j)
{
for (i = l; i + (1 << j) - 1 <= r; ++i)
{
Max[i][j] = max(Max[i][j - 1], Max[i + (1 << (j - 1))][j - 1]);
Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);
}
}
}
pii ST(int l, int r)
{
int k = log2(r - l + 1);
int Ma = max(Max[l][k], Max[r - (1 << k) + 1][k]);
int Mi = min(Min[l][k], Min[r - (1 << k) + 1][k]);
return pii(Ma, Mi);
}
int main(void)
{
int tcase;
scanf("%d", &tcase);
while (tcase--)
{
int n, k, i;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
rmq_init(1, n); LL ans = 0;
for (i = 1; i <= n; ++i)
{
int L = i, R = n;
int idx = i;
while (L <= R)
{
int mid = MID(L, R);
pii temp = ST(i, mid);
if (temp.first - temp.second < k)
{
idx = mid;
L = mid + 1;
}
else
R = mid - 1;
}
ans += (LL)(idx - i + 1);
}
printf("%I64d\n", ans);
}
return 0;
}

HDU 5289 Assignment(二分+RMQ-ST)的更多相关文章

  1. HDU 5289——Assignment——————【RMQ+优化求解】

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

  2. HDU 5289 Assignment [优先队列 贪心]

    HDU 5289 - Assignment http://acm.hdu.edu.cn/showproblem.php?pid=5289 Tom owns a company and he is th ...

  3. HDU 5289 Assignment (二分+区间最值)

    [题目链接]click here~~ [题目大意]: 给出一个数列,问当中存在多少连续子序列,子序列的最大值-最小值<k [思路]:枚举数列左端点.然后二分枚举右端点,用ST算法求区间最值.(或 ...

  4. HDU 5289 Assignment (ST算法区间最值+二分)

    题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...

  5. HDU - 5289 Assignment (RMQ+二分)(单调队列)

    题目链接: Assignment  题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...

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

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

  7. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

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

  8. hdu 5289 Assignment (ST+二分)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

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

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

随机推荐

  1. 19课 Vue第二节

    事件修饰符 stop 禁止冒泡once 单次事件prevent 阻止默认事件native 原生事件(组件)keycode|name 筛选按键   组合键 : @keydown.ctrl.enter s ...

  2. RabbitMQ-消费者"未处理完的消息"丢失

    一个关于客户端(消费者)开启自动应答,重启后"未处理消息丢失"的小坑.(主要是对RabbitMQ理解不够) 首先,申明一下: 本文所谓的 "丢失消息" 不是指服 ...

  3. C++ 学习笔记 (八)重载 重写 重定义以及名字覆盖

    学习C++必定会遇到重载.重写.重定义.概念的东西多也是学习C++蛋疼之处,但是还是得弄懂,学懂了也就不觉得多了. 概念,特点: 重载: 直白点说就是函数名字相同,传参的个数,类型不一样.判断标准在于 ...

  4. 破解studio 3T

    方法一: 打开注册表:regedit 计算机\HKEY_CURRENT_USER\Software\JavaSoft\Prefs\3t\mongochef\enterprise 将里面得数据清零,又是 ...

  5. HTML5/CSS3 第三章页面布局

    页面布局 1 页面组成 2 布局相关的标签 <div></div> 定义文档中的分区或节 <span></span> 这是一个行内元素,没有任何意义 & ...

  6. day03_基本数据类型基本运算

    1.什么是数据类型 变量值才是我们存储的数据,所以数据类指的就是变量值的不同种类 2.为何数据要分类型? 变量值是用来保存现实世界中的状态的,那么针对不同的状态就应该用不同类型的数据去表示 3.如何用 ...

  7. thinkphp 3.2.3 - Route.class.php 解析(路由匹配)

    class Route { public static function check(){ $depr = C('URL_PATHINFO_DEPR'); // '/' $regx = preg_re ...

  8. linux硬件基础

    1. 服务器分类 机架式服务器(主要用这个). 刀片式服务器. 塔式服务器. 2. 机架式服务器 服务器的尺: U - 2U. 服务器核心之电源: 双电源 AB 路. 服务器核心之 CPU-计算 CP ...

  9. 使用Hbase快照将数据输出到互联网区测试环境的临时Hbase集群

    通过snapshot对内网测试环境Hbase生产集群的全量数据(包括原始数据和治理后数据)复制到互联网Hbase临时集群.工具及原理: 1)         Hbase自带镜像导出工具(snapsho ...

  10. 1001: [BeiJing2006]狼抓兔子(对偶图)

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 23595  Solved: 5940 Descript ...