HDU 5289 - Assignment

http://acm.hdu.edu.cn/showproblem.php?pid=5289 
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:a1,a2,…,an(0 <= ai <= 10^9), indicate the i-th staff’s ability. 
Output 
For each test,output the number of groups. 
Sample Input 

4 2 
3 1 2 4 
10 5 
0 3 4 5 2 1 6 7 8 9 
Sample Output 

28

这题就是给定一个序列,求出满足区间内最大值最小值之差小于k的子序列的数量。

我是用优先队列每次更新区间左端点,一遍for循环写的。队里的dalao是用RMQ先预处理完然后二分做的。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define eps 1e-8
using namespace std;
typedef long long ll; const int maxn = 1e5+;
int arr[maxn],n,k; struct node1{
int pos,val;
node1(int _pos,int _val):pos(_pos),val(_val){}
bool operator < (const node1 &c) const{
return (val < c.val);
}
};
struct node2{
int pos,val;
node2(int _pos,int _val):pos(_pos),val(_val){}
bool operator < (const node2 &c) const{
return (val > c.val);
}
};
priority_queue <node1> high;
priority_queue <node2> low; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
while(!high.empty()) high.pop();
while(!low.empty()) low.pop();
scanf("%d%d",&n,&k);
for(int i=;i<n;i++)
scanf("%d",&arr[i]);
ll ans = ;
int l = , MIN = INF, MAX = -INF;//, pmi = 0, pma = 0;
for(int i=;i<n;i++)
{
MIN = min(MIN,arr[i]);
MAX = max(MAX,arr[i]);
high.push(node1(i,arr[i]));
low.push(node2(i,arr[i]));
if(MAX - MIN >= k)
{
if(arr[i] == MAX)
{
while(!low.empty() && arr[i] - MIN >= k)
{
l = max(l,low.top().pos + );
low.pop();
MIN = low.top().val;
}
}
else
{
while(!high.empty() && MAX - arr[i] >= k)
{
l = max(high.top().pos + ,l);
high.pop();
MAX = high.top().val;
}
}
}
ans += i-l+;
}
printf("%lld\n",ans-);
}
}
 

HDU 5289 Assignment [优先队列 贪心]的更多相关文章

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

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

  2. HDU 5289 Assignment rmq

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

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

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

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

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

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

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

  6. hdu 5360 Hiking(优先队列+贪心)

    题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L, ...

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

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

  8. HDU - 5289 Assignment (RMQ+二分)(单调队列)

    题目链接: Assignment  题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...

  9. HDU 5289——Assignment——————【RMQ+优化求解】

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

随机推荐

  1. Ubuntu 终端配置

    安装zsh apt install zsh 设置zsh为默认shell chsh -s /bin/zsh 恢复 bash 使用 chsh -s /bin/bash 安装 Oh My Zsh sh -c ...

  2. python之子类调用父类的两种方式

    第一种方式 直接在子类中调用父类名: Vehicle.__init__(self,name,speed,load,power)#调用父类的实例 Vehicle.run(self) #调用父类的方法 # ...

  3. 记录python爬取猫眼票房排行榜(带stonefont字体网页),保存到text文件,csv文件和MongoDB数据库中

    猫眼票房排行榜页面显示如下: 注意右边的票房数据显示,爬下来的数据是这样显示的: 网页源代码中是这样显示的: 这是因为网页中使用了某种字体的缘故,分析源代码可知: 亲测可行: 代码中获取的是国内票房榜 ...

  4. 【codeforces 723E】One-Way Reform

    [题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...

  5. PHP学习总结(9)——PHP入门篇之WAMPServer服务控制面板介绍

    及MySQL数据库的整合软件包.免去了开发人员将时间花费在繁琐的配置环境过程,从而腾出更多精力去做开发.在windows下将Apache+PHP+Mysql 集成环境,拥有简单的图形和菜单安装和配置环 ...

  6. structs中通过LabelValueBean构建下拉列表

    Action类中增加列表 List<LabelValueBean> list = new ArrayList<LabelValueBean>(); list.add(new L ...

  7. byte类型取值范围以及溢出具体解释

    例1: public class test { public static void main(String[] args) { byte a = 127 ; a = (byte)(a+3) ; Sy ...

  8. js代码从页面移植到文件里失效或js代码改动后不起作用的解决的方法

    近期在做关于站点的项目,总是发生这种问题 写的javascript代码在页面上没有问题,可是将js代码移植到.js的文件里,在页面上进行调用,总是出现失效等错误 另外改动后的js代码,又一次刷新网页仍 ...

  9. Web前端开发实战2:二级下拉式菜单之JS实现

    上一篇博文提到了二级下拉式菜单是用HTML和CSS实现的.我们这一篇来用JavaScript脚本实现下拉菜单的显 示和隐藏. 使用 JavaScript方法实现我们须要用的知识有: 1)JS事件:on ...

  10. wpf获取目录路径

    AppDomain.CurrentDomain.BaseDirectory +文件名即可,简单吧? //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. string str5=Appl ...