Assignment

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

Total Submission(s): 1870    Accepted Submission(s): 916

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:  5315 5314 5313 

pid=5312" target="_blank">5312 

pid=5311" target="_blank">5311 

 题目大意:输入n,k,问n个数的序列。有多少个区间,最大值减最小值小于k
暴力找区间超时一次
ac代码,跑了1000ms
#include<stdio.h>
#include<string.h>
#include<math.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
int n;
__int64 m;
__int64 a[100010];
__int64 maxv[100010][20],minv[100010][20];
void init()
{
int i,j,k;
for(i=1;i<=n;i++)
{
minv[i][0]=a[i];
maxv[i][0]=a[i];
}
for(j=1;(1<<j)<=n;j++)
{
for(k=0;k+(1<<j)-1<=n;k++)
{
minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);
maxv[k][j]=max(maxv[k][j-1],maxv[k+(1<<(j-1))][j-1]);
}
}
}
__int64 q_max(int l,int r)
{
int k=(int)(log((double)(r-l+1))/log(2.0));
return max(maxv[l][k],maxv[r-(1<<k)+1][k]);
}
__int64 q_min(int l,int r)
{
int k=(int)(log((double)(r-l+1))/log(2.0));
return min(minv[l][k],minv[r-(1<<k)+1][k]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%I64d",&n,&m);
int i,j,k;
for(i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
init();
__int64 ans=0;
int p=1;
/*for(i=1;i<=n;i++)//暴力超时
{
for(j=i;j<=n;j++)
{
if(q_max(i,j)-q_min(i,j)<m)
ans++;
}
}*/
for(i=1;i<=n;i++)
{
while(q_max(p,i)-q_min(p,i)>=m&&p<i)
{
p++;
}
ans+=i-p+1;
}
printf("%I64d\n",ans);
}
}

网上看到用deque做的,跑了577ms,收藏一下,http://www.bubuko.com/infodetail-987302.html

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std ;
#define LL __int64
deque <LL> deq1 , deq2 ;
//单调队列,deq1最大值,deq2最小值
LL a[100010] ;
int main() {
int t , n , i , j ;
LL k , ans ;
scanf("%d", &t) ;
while( t-- ) {
scanf("%d %I64d", &n, &k) ;
for(i = 0 ; i < n ; i++)
scanf("%I64d", &a[i]) ;
if(k == 0) {
printf("0\n") ;
continue ;
}
while( !deq1.empty() ) deq1.pop_back() ;
while( !deq2.empty() ) deq2.pop_back() ;
for(i = 0 , j = 0 , ans = 0; i < n ; i++) {//i在前。j在后
while( !deq1.empty() && deq1.back() < a[i] ) deq1.pop_back() ;
deq1.push_back(a[i]) ;
while( !deq2.empty() && deq2.back() > a[i] ) deq2.pop_back() ;
deq2.push_back(a[i]) ;
while( !deq1.empty() && !deq2.empty() && deq1.front() - deq2.front() >= k ) {
ans += (i-j) ;
//printf("%d %d,%I64d %I64d\n", i , j, deq1.front() , deq2.front() ) ;
if( deq1.front() == a[j] ) deq1.pop_front() ;
if( deq2.front() == a[j] ) deq2.pop_front() ;
j++ ;
}
}
while( j < n ) {
ans += (i-j) ;
j++ ;
}
printf("%I64d\n", ans) ;
}
return 0 ;
}

HDOJ 题目5289 Assignment(RMQ,技巧)的更多相关文章

  1. HDU 5289 Assignment rmq

    Assignment 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Description Tom owns a company and h ...

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

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

  3. 二分+RMQ/双端队列/尺取法 HDOJ 5289 Assignment

    题目传送门 /* 题意:问有几个区间最大值-最小值 < k 解法1:枚举左端点,二分右端点,用RMQ(或树状数组)求区间最值,O(nlog(n))复杂度 解法2:用单调队列维护最值,O(n)复杂 ...

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

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

  5. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  6. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

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

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

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

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

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

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

随机推荐

  1. dwarfdump --arch=arm64 --lookup

    解析友盟错误信息重要指令: dwarfdump --arch=arm64 --lookup 0x1001edbc4 /Users/zhoujunbo/Library/Developer/Xcode/A ...

  2. Vue核心知识-computed和watch的使用场景和方法

    https://www.jianshu.com/p/bb7a2244c7ca

  3. 反转链表_JAVA

    package algorithms; /* * * * 输入一个链表,反转链表后,输出新链表的表头. * public class ListNode { int val; ListNode next ...

  4. csr_matrix参数解析

    压缩稀疏矩阵构造时的参数从官网看不明白,参考如下: >>> indptr = np.array([0, 2, 3, 6]) >>> indices = np.arr ...

  5. HDU - 5952 Counting Cliques(dfs搜索)

    题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...

  6. restful风格url Get请求查询所有和根据id查询的合并成一个controller

    restful风格url Get请求查询所有和根据id查询的合并成一个controller的方法 原代码 // 127.0.0.1:8080/dep/s @ApiOperation(value=&qu ...

  7. git添加user及repository

  8. Docker从入门到实践

    一般说来 SPA 的项目我们只要启一个静态文件 Server 就可以了,但是针对传统项目就不一样了,一个项目会依赖很多服务端程序.之前我们的开发模式是在一台开发机上部署开发环境,所有人都在这台开发机上 ...

  9. ubuntu 安装codeblocks13.12

    通过添加PPA的方法安装: sudo add-apt-repository ppa:pasgui/ppa sudo apt-get update sudo apt-get install codebl ...

  10. Python基础之函数参数与返回值进阶

    参数作用:如果外界希望在函数内部处理数据,就可以将数据作为参数传入函数内部: 返回值作用:如果希望一个函数函数执行完成后,向外界报告函数的执行结果,就可以使用函数的返回值. 函数的返回值 进阶 利用元 ...