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

Total Submission(s): 2555    Accepted Submission(s): 1200


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]

这题有多种做法,但思路差不多,我的做法是依次枚举左端点或者右端点,然后二分查找所能达到的最右边,然后累加起来。

方法一:用树状数组或者rmq枚举左端点,然后二分查找满足条件的最右端点。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100060
#define ll long long
#define inf 2000000000
int a[maxn],b1[maxn],b2[maxn];/*b1最小值,b2最大值*/
int lowbit(int x){
return x&(-x);
}
void update1(int pos,int num)
{
while(pos<=maxn){
b1[pos]=min(b1[pos],num);pos+=lowbit(pos);
}
} int question1(int pos)
{
int num=inf;
while(pos>0){
num=min(b1[pos],num);pos-=lowbit(pos);
}
return num;
} void update2(int pos,int num)
{
while(pos<=maxn){
b2[pos]=max(b2[pos],num);pos+=lowbit(pos);
}
} int question2(int pos)
{
int num=-1;
while(pos>0){
num=max(b2[pos],num);pos-=lowbit(pos);
}
return num;
} int main()
{
int n,m,i,j,T,k,l,mid,r;
ll ans;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++){
b1[i]=inf;b2[i]=-1;
scanf("%d",&a[i]);
}
ans=0;
for(i=n;i>=1;i--){
update1(i,a[i]);
update2(i,a[i]);
l=i;r=n;
while(l<=r){
mid=(l+r)/2;
if(question2(mid)-question1(mid)>=k){
r=mid-1;
}
else l=mid+1;
}
//printf("%d %d %d\n",i,l,r);
if(r>=i)
ans+=(ll)(r-i+1);
/*printf("%lld\n",ans);*/
}
printf("%lld\n",ans);
}
return 0;
}

代码二:维护两个单调队列,分别维护最大值和最小值,依次枚举右端点,然后找到符合条件的左端点。(这个方法速度是最快的,只需243ms,惊!)

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100060
#define ll long long
int q1[1111111][2],q2[1111111][2];
int a[maxn];
int main()
{
int n,m,i,j,k,front1,front2,rear1,rear2,l,r,T;
ll ans;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
ans=0; front1=front2=1;rear1=rear2=0;l=1;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
while(front1<=rear1 && q1[rear1][0]>=a[i]){
rear1--;
}
rear1++;q1[rear1][0]=a[i];q1[rear1][1]=i; while(front2<=rear2 && q2[rear2][0]<=a[i]){
rear2--;
}
rear2++;q2[rear2][0]=a[i];q2[rear2][1]=i;
while(front1<=rear1 && front2<=rear2 && q2[front2][0]-q1[front1][0]>=k){
l=min(q2[front2][1],q1[front1][1])+1;
if(q2[front2][1]<q1[front1][1]){
front2++;
}
else if(q2[front2][1]>q1[front1][1]){
front1++;
}
else{
front1++;front2++;
}
}
ans+=i-l+1;
/*printf("%d %d %lld\n",i,l,ans);*/
}
printf("%lld\n",ans); }
return 0;
}

hdu5289 Assignment的更多相关文章

  1. hdu5289 Assignment (区间查询最大值最小值,st算法...)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给定长度为n的序列a和一个整数K,找出最大值和最小值的差值小于K的区间.输出满足条件的区间的个 ...

  2. hdu5289(2015多校1)--Assignment(单调队列)

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

  3. hdu5289 2015多校联合第一场1002 Assignment

    题意:给出一个数列.问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k 思路:设dp[i]为从区间1到i满足题意条件的解.终于解即为dp[n]. 此外 如果对于arr[i] 往左遍历 ...

  4. 【HDU5289】Assignment

    题目大意:给定一个长度为 N 的序列,求序列中最大值和最小值相差小于 K 的连续段的个数. 题解: 最大值和最小值相差不超过 K 是一个在值域角度的限制,应考虑采用平衡树或权值...数据结构进行维护. ...

  5. Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为“通用职责分配软件模式”

    Atitit GRASP(General Responsibility Assignment Software Patterns),中文名称为"通用职责分配软件模式" 1. GRA ...

  6. user initialization list vs constructor assignment

    [本文连接] http://www.cnblogs.com/hellogiser/p/user_initialization_list.html [分析] 初始化列表和构造函数内的赋值语句有何区别? ...

  7. Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removing it

    Swift 提示:Initialization of variable was never used consider replacing with assignment to _ or removi ...

  8. 代写assignment

    集英服务社,强于形,慧于心 集英服务社,是一家致力于优质学业设计的服务机构,为大家提供优质原创的学业解决方案.多年来,为海内外学子提供了多份原创优质的学业设计解决方案. 集英服务社,代写essay/a ...

  9. [Top-Down Approach] Assignment 1: WebServer [Python]

    Today I complete Socket Programming Assignment 1 Web Server Here is the code: #!/usr/bin/python2.7 # ...

随机推荐

  1. spring cloud gateway 日志打印

    从api请求中获取访问的具体信息,是一个很常见的功能,这几天在研究springcloud,使用到了其中的gateway,刚好将研究的过程结果都记录下来 0. Version <parent> ...

  2. .NET Core部署到linux(CentOS)最全解决方案,常规篇

    本文为大家介绍使用 .NET Core部署到Linux服务器的方法,通过本文你将了解到Linux在虚拟机下的安装.Xshell,Xftp的使用方法.git在linux下的交互使用以及.net core ...

  3. for update语句锁机制问题

    数据库小知识学习系列 问题: MySQL InnoDB中,select where xxx=123 for update:该xxx没有索引,是使用表锁还是全部数据加行锁? 答: InnoDB引擎(默认 ...

  4. kubernets与API服务器进行交互

    一  为何需要与kubernets集群的API服务器进行交互 1.1  kubernets提供了一种downapi的资源可以将pod的元数据渲染成环境变量或者downward卷的形式挂载到容器的文件系 ...

  5. AQS之ReentrantReadWriteLock精讲分析上篇

    1.用法 1.1 定义一个安全的list集合 public class LockDemo { ArrayList<Integer> arrayList = new ArrayList< ...

  6. 小白的经典CNN复现(二):LeNet-5

    小白的经典CNN复现(二):LeNet-5 各位看官大人久等啦!我胡汉三又回来辣(不是 最近因为到期末考试周,再加上老板临时给安排了个任务,其实LeNet-5的复现工作早都搞定了,结果没时间写这个博客 ...

  7. Centos7 虚拟机优化

    配置yum源 rm -f /etc/yum.repos.d/* curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/ ...

  8. Apache Unomi 远程代码执行漏洞复现(CVE-2020-13942)

    一.漏洞描述 Apache Unomi 是一个基于标准的客户数据平台(CDP,Customer Data Platform),用于管理在线客户和访客等信息,以提供符合访客隐私规则的个性化体验.在Apa ...

  9. Netty之Unpooled_Bytebuf

    前言 计算机存储基本单位是字节(byte),传输基本单位是bit(位),JAVA NIO提供了ByteBuffer等七种容器来提升传输时的效率,但是在使用时比较复杂,经常要进行读写切换,主要缺点如下: ...

  10. Mybatis报错:Could not find resource mybatis-conf.xml

    Mybatis报错:Could not find resource mybatis-conf.xml 报错截图: 报错内容: java.io.IOException: Could not find r ...