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. Python基础语法5-控制流语句

  2. kubernets之pod的标签拓展

    一 标签的拓展使用 1.1 标签的作用范围不仅仅适用于pod对node以及其他类的大部分资源同样适用 k label node node01 gpu=true k是kubectl的别名形式 同样对于n ...

  3. ctfhub技能树—RCE—过滤目录分隔符,过滤运算符

    过滤目录分隔符 打开靶机 查看页面信息 查询当前目录下文件结构 进入flag_is_here目录 127.0.0.1;cd flag_is_here 127.0.0.1||ls 执行之后发现还是在当前 ...

  4. ABAP-ALV-如何去掉OO方法中的ALV的标准按钮

    SAP在做报表开发中,不同公司对报表的风格往往各异,为此经常在使用OO方法做ALV报表中需要去掉自带的工具栏而自行添加一些工具按钮,下面将简单介绍一些其实现过程与原理: 步骤一: DATA : gt_ ...

  5. 零基础怎么学Python编程,新手常犯哪些错误?

    Python是人工智能时代最佳的编程语言,入门简单.功能强大,深获初学者的喜爱. 很多零基础学习Python开发的人都会忽视一些小细节,进而导致整个程序出现错误.下面就给大家介绍一下Python开发者 ...

  6. pytorch——预测值转换为概率,单层感知机

    softmax函数,可以将算出来的预测值转换成0-1之间的概率形式 导数的形式 import torch import torch.nn.functional as F x=torch.tensor( ...

  7. linux通过ntpd同步服务器时间,

    ntpd得rpm包下载地址:https://pkgs.org/download/ntp 比如我得服务器版本是centos7 x86的,那选择我点击的这一个: 下拉到最下面就有安装包下载了,我选择的是二 ...

  8. 通俗易懂的解释:什么是API

    API 全称 Application Programming Interface,即应用程序编程接口. 看到这里,急性子的小白同学马上就憋不住了:这不管是英文还是中文我每个字都懂啊,只是凑一块就不知道 ...

  9. moco框架加入cookies

    一.带cookie信息的get请求 注意:cookie是放在request里的,一般登录的场景这些会用到 1.代码 2.接口管理工具添加 注意:cooike的域和路径都要添加 二.带cookie信息的 ...

  10. gin框架之路由前缀树初始化分析

    https://mp.weixin.qq.com/s/lLgeKMzT4Q938Ij0r75t8Q