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. 你必须要懂的 Github 开源协议

    作为一个开源社区的活跃者,那些开源协议你都懂什么意思吗? 列两个: Apache License 可以: 商用.修改.分发 但是要声明作者来源和你的修改以及协议 MIT  License 只要声明版权 ...

  2. Docker 镜像基础(三)

    基于Dockerfile制作yum版本nginx镜像 [root@node-2 ~]# mkdir /opt/nginx [root@node-2 ~]# cd /opt/nginx/ ## 创建Do ...

  3. P2986 [USACO10MAR]伟大的奶牛聚集(思维,dp)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  4. mysql:如何解决数据修改冲突(事务+行级锁的实际运用)

    摘要:最近做一个接诊需求遇到一个问题,假设一个订单咨询超过3次就不能再接诊,但如果两个医生同时对该订单进行咨询,查数据库的时候查到的接诊次数都是2次,那两个医生都能接诊,所谓接诊可以理解为更新了接诊次 ...

  5. 24V转5V稳压芯片,高效率的同步降压DC-DC变换器3A输出电流

    PW2330开发了一种高效率的同步降压DC-DC变换器3A输出电流.PW2330在4.5V到30V的宽输入电压范围内工作集成主开关和同步开关,具有非常低的RDS(ON)以最小化传导损失.PW2330采 ...

  6. Development desciptor

    概述与作用: 部署描述符是用于描述Web应用程序的元数据,并为Java EE Web应用程序服务器部署和运行Web应用程序提供指令.从传统上来说,所有元数据都来自于部署描述符文件/WEB-INF/we ...

  7. Ice系列--基于IceGrid的部署方案

    前言 前一篇文章介绍了IceGrid的简单应用.这篇文章来介绍一下它的高端玩法-如何将模板,复制组,知名对象应用于部署方案及其作用. 基于模板的部署方案 之前介绍了xml格式的配置文件通过各种描述符如 ...

  8. Python数据模型与Python对象模型

    数据模型==对象模型 Python官方文档说法是"Python数据模型",大多数Python书籍作者说法是"Python对象模型",它们是一个意思,表示&quo ...

  9. Spark底层原理详细解析(深度好文,建议收藏)

    Spark简介 Apache Spark是用于大规模数据处理的统一分析引擎,基于内存计算,提高了在大数据环境下数据处理的实时性,同时保证了高容错性和高可伸缩性,允许用户将Spark部署在大量硬件之上, ...

  10. 【实战】ZooKeeper 实战

    1. 前言 这篇文章简单给演示一下 ZooKeeper 常见命令的使用以及 ZooKeeper Java客户端 Curator 的基本使用.介绍到的内容都是最基本的操作,能满足日常工作的基本需要. 如 ...