题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289

Assignment

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

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]

 
Author
FZUACM
 
Source
 
Recommend
We have carefully selected several similar problems for you:  5669 5668 5667 5666 5665 
 
题意:给你一个数列,再给你一个k,问存在多少个连续子序列使得子序列的最大最小值差值 小于 k.
题解:
贪心+ST表
枚举右端点,因为左端点一定是递增或不变的,所以遇到枚举的区间内的最大最小值差值 大于等于 k,就将左端点加1。然后每次统计个数即可。
注意:答案要开long long。
 #include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
int n,mn[MAXN][],mx[MAXN][],a[MAXN];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void ST()
{
int i,j;
for(i=;i<=n;i++)mn[i][]=mx[i][]=a[i];
for(j=;(<<j)<=n;j++)
{
for(i=;i+(<<j)-<=n;i++)
{
mn[i][j]=min(mn[i][j-],mn[i+(<<(j-))][j-]);
mx[i][j]=max(mx[i][j-],mx[i+(<<(j-))][j-]);
}
}
}
int Query(int l,int r)
{
int j;
for(j=;(<<j)<=(r-l+);j++);j--;
return max(mx[l][j],mx[r-(<<j)+][j])-min(mn[l][j],mn[r-(<<j)+][j]);
}
int main()
{
int T,k,i,left,right;
long long ans;
T=read();
while(T--)
{
n=read();k=read();
for(i=;i<=n;i++)a[i]=read();
ST();
left=;//左端点(左端点一定是单调递增的)
ans=;
for(right=;right<=n;right++)//枚举右端点
{
while(Query(left,right)>=k&&left<right)left++;//在枚举右端点的同时,移动左端点.每次改变右端点时,左端点只可能不变或向右移动.
ans+=((long long)right-left+1LL);//统计的子串为以右端点为最右端,最左端在 右端点->左端点 之间.
}
printf("%lld\n",ans);
}
fclose(stdin);
fclose(stdout);
return ;
}

Hdu 5289-Assignment 贪心,ST表的更多相关文章

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

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

  2. HDU 5289 Assignment [优先队列 贪心]

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

  3. [BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆)

    [BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆) 题面 给出一个长度为n的序列,选k段长度在L到R之间的区间,一个区间的值等于区间内所有元素之的和,使得k个区间的值之和最大.区 ...

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

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

  5. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

  6. HDU 5289 Assignment (数字序列,ST算法)

    题意: 给一个整数序列,多达10万个,问:有多少个区间满足“区间最大元素与最小元素之差不超过k”.k是给定的. 思路: 如果穷举,有O(n*n)复杂度.可以用ST算法先预处理每个区间最大和最小,O(n ...

  7. hdu 5289 Assignment (ST+二分)

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

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

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

  9. HDU 5289 Assignment rmq

    Assignment 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Description Tom owns a company and h ...

随机推荐

  1. 搭建 Android 开发环境,初试HelloWorld (win7) (上) (转)

    搭建Android开发环境主要有以下几步要做: 1.JDK安装 2.Eclipse安装 3.Android SDK安装 4.ADT安装 5.创建AVD 1.JDK(Java Development K ...

  2. spoj 346

    当一个数大于等于12  那分别处以2, 3, 4之后的和一定大于本身    但是直接递归会超时    然后发现有人用map存了   膜拜..... #include <cstdio> #i ...

  3. State Management

    Samza的task可以把数据进行本地存储,并且对这些数据进行丰富的查询.   比较SQL中的select ... where...并不需要保存状态.但是aggregation和join就需要存储ro ...

  4. express 3.0.x 中默认不支持layout.ejs的解决方法

    1.第一种方法用include 用<% include 模板名(可不加.ejs) %>的写法,具体如下 <% include header %> //套用布局拆成两部分 hea ...

  5. Generating Huge reports in JasperReports

    There are certain things to care while implementing the Jasper Reports for huge dataset to handle th ...

  6. 【leetcode】4Sum(middle)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  7. cocos2d-x 多分辨率适配详解(转载),以前北京团队设计的游戏,也是用这套方案

    http://blog.csdn.net/kyo7552/article/details/17163487 多种分辨率的适配一直都是一个蛋疼的问题,各家公司可能都有自己的一套方案.今天我为大家介绍的是 ...

  8. Servlet课程0425(七) 到数据库中去验证用户,同时防止SQL注入漏洞

    Login.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class ...

  9. SQLite入门与分析(三)---内核概述(2)

    写在前面:本节是前一节内容的后续部分,这两节都是从全局的角度SQLite内核各个模块的设计和功能.只有从全局上把握SQLite,才会更容易的理解SQLite的实现.SQLite采用了层次化,模块化的设 ...

  10. linux系统的crond服务

    linux系统中有一个服务,用来做周期性运行的例行任务,这个服务就是crond服务.执行这项服务的命令 就是crontab命令了.而linux下的任务调度又分为系统任务调度和用户任务调度两个大类. 系 ...