HDU 5289 Assignment(RMQ+二分)
2016-06-13 21:48
405 查看
Assignment
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3338 Accepted Submission(s): 1549
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
(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
HintFirst Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
Author
FZUACM
Source
2015 Multi-University Training Contest 1
Recommend
We have carefully selected several similar problems for you: 5717 5716 5715 5714 5713
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
int maxn[100000][30];
int minn[100000][30];
int a[100000];
int n,k;
void RMQ_init()
{
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)
{
maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
}
}
}
int query(int l,int r)
{
int k=log2(r-l+1);
return max(maxn[l][k],maxn[r-(1<<k)+1][k])-min(minn[l][k],minn[r-(1<<k)+1][k]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
maxn[i][0]=minn[i][0]=a[i];
}
RMQ_init();
long long ans=0;
int l,r;
for(int i=1;i<=n;i++)
{
l=i,r=n;
while(l<=r)
{
int mid=(l+r)/2;
int cha=query(i,mid);
if(cha<k)l=mid+1;
else r=mid-1;
}
ans=ans+l-i;
}
printf("%lld\n",ans);
}
}
相关文章推荐
- java编程最佳实践
- Spring 表单标签
- c++:人数自定的工资类
- 哈哈呜呜交替显示
- Java模块 -- 计算工作日 (Excel导入法定假日至数据库 排除周六 周日 法定假日) Calendar
- C++常用类型转换
- git shell 推送到github网站
- PHP中单引号和双引号的区别
- 从零开始做UI-静电的sketch设计教室 视频教程
- JAVA几种缓存技术介绍说明
- 开源新闻速递:使用 GTK3 构建的 MATE 桌面更节约内存
- Oracle学习 第5天之高级查询
- 同源策略
- 关于iOS9中的App Transport Security相关说明及适配
- iOS开发--录音简单实现
- 剑指offer----斐波那契数列的实现--递归和迭代
- C++ this指针详解
- MySQL5.7安装过程以及参数和设置说明
- CodeIgniter学习笔记 Item3--CI中的超级对象
- Lightoj 1010 - Knights in Chessboard