Assignment

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

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]

 
题目大意:给你t组测试数据。每组有n,k。表示有序列长度为n,求有多少个连续的区间满足区间内的任意元素之差小于k。
解题思路:现在用i,j分别表示左右区间位置。然后从左往右扩展,如果要扩展的那个元素跟区间内的任意元素差值都小于k,则res++,且区间向右扩展一个长度。直到遇到元素Aj,跟区间内的某个元素发生冲突,那么我停止向右扩展这个Aj。然后我让原来的区间[i,j-1]左区间向右移动一个位置。我向右移动左区间,那么这个区间内的符合的区间个数必然比刚才的[i,j-1]这个区间少一个,那么res--。然后这时候继续扩展上次发生冲突的位置j,看现在的区间是否能跟Aj不发生冲突。然后一直重复地操作。简单总结就是如果右区间能扩展,就扩展;不能扩展,让左区间向右移动一个位置,然后看现在的区间能不能让右区间向右扩展。
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INT long long
const int maxn=1e5+20;
int d[maxn][50];
int dp[maxn][50];
int A[maxn];
int Abs(int x){
return x<0? -x:x;
}
void RMQ_init(int n){//RMQ维护出区间i,j之间的最大最小值。
for(int i=0;i<n;i++){
d[i][0]=A[i];
dp[i][0]=A[i];
}
for(int j=1;(1<<j)<=n;j++){
for(int i=0;i+(1<<j)-1<n;i++){
d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
}
int RMQ(int L,int R,int typ){
int k=0;
while((1<<(k+1)<=R-L+1))
k++;
if(typ==0)
return min(d[L][k],d[R-(1<<k)+1][k]);
else
return max(dp[L][k],dp[R-(1<<k)+1][k]);
}
int main(){
int t,i,j,k,n,tmp,ret,itvlav,itvliv,sum,flag;
INT res,ans;
scanf("%d",&t);
while(t--){
memset(dp,0,sizeof(dp));
memset(d,0,sizeof(d));
memset(A,0,sizeof(A));
scanf("%d%d",&n,&k);
for(i=0;i<n;i++){
scanf("%d",&A[i]);
}
RMQ_init(n);
tmp=0,res=0,sum=0;
itvlav=itvliv=A[0];
for(i=0;i<n;i++){
flag=0;
for(j=tmp;j<n;j++){
if(Abs(itvlav-A[j])<k&&Abs(itvliv-A[j])<k){//右区间能向右扩展
tmp=j+1;
itvlav=RMQ(i,j,1);
itvliv=RMQ(i,j,0);
res++;
}else{ //右区间现在无法扩展,向右移动左区间位置,看新的区间是否能向右扩展
flag=1;
tmp=j;
itvlav=RMQ(i+1,j-1,1);
itvliv=RMQ(i+1,j-1,0);
break;
}
}
sum+=res;
res--;
if(flag==1){
if(res==0){
itvlav=itvliv=A[tmp];
}
} }
printf("%lld\n",sum);
}
return 0;
}

  

HDU 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. HDU 5289 Assignment [优先队列 贪心]

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

  4. HDU 5289 Assignment(二分+RMQ-ST)

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

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

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

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

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

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

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

  8. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

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

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

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

随机推荐

  1. 接上一篇,Springcloud使用feignclient远程调用服务404 ,为什么去掉context-path后,就能够调通

    一.问题回顾 如果application.properties文件中配置了 #项目路径 server.servlet.context-path=/pear-cache-service 则feigncl ...

  2. Mongoid Relationships

    1:1 embedded relationship: M:1 linked relationship(one_direction): child uses belongs_to parent uses ...

  3. C语言实现数组及链表的快速排序

    1. 数组快排: 方法一: #include <stdio.h> #include <stdlib.h> //交换 void swap(int *pi, int *pj) { ...

  4. c语言和c++的相互调用

    1.c与c++编译方式 (1)gcc和g++都可以编译.c文件,也都可以编译.cpp文件.g++和gcc是通过后缀名来辨别是c程序还是c++程序的(这一点与Linux辨别文件的方式不同,Linux是通 ...

  5. C++_异常1-调用abort()

    异常是相对较新的C++功能,有些老式编译器可能没有实现.另外有些编译器可能默认关闭这一特性,需要使用编译器选项来打开它. 这里先讨论一个基本问题: 2.0 * x * y / (x+y) 如果y是x的 ...

  6. LeetCode记录之27——Remove Element

    这道题跟26题很类似,并且有官方的答案.看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄.有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms. ive ...

  7. POJ_2010 Moo University - Financial Aid 【堆预处理】

    一.题面 POJ2010 二.分析 堆预处理 首先可以考虑吧随便取一个点,判断两侧的最小的总费用是多少,然后相加判断是否满足条件.如果直接判断会超时,所以需要用大根堆预处理一下.先看从分数最小的往最大 ...

  8. BZOJ - 1257 分块 详解

    中文题面 这道题就是LightOJ某题的升级版 前段时间我是直接用√k前暴力后分块的处理方式,然后直接套个等差求和 这次看到了dalao的证明再次让我知道我好菜啊 在这里做下笔记,学习一下对于整除运算 ...

  9. JPEG 编码

    WIN8. DNJXJ-7XBW8-2378T-X22TX-BKG7J 模板:类的宏,泛型,甜饼切割机 类模板:泛型类: 函数模板:泛型函数 STL standard template Library ...

  10. 不要重复发明轮子-C++STL

    闫常友 著. 中国铁道出版社. 2013/5 标准的C++模板库,是算法和其他一些标准组件的集合. . 1.类模板简介 2.c++中的字符串 3.容器 4.c++中的算法 5.迭代器 6.STL 数值 ...