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. [codeforce 975C] Valhalla Siege (二分)

    Examples input 5 5 1 2 1 2 1 3 10 1 1 1 output 3 5 4 4 3 input 4 4 1 2 3 4 9 1 10 6 output 1 4 4 1 N ...

  2. docker-compose,docker-machine,docker swarm 的简单总结

    1.docker-compose: 用来在单机上启动一组应用服务.这个服务中可能包含有很多应用,比如你的app服务,redis,mysql,等等. (1)我们需要先创建好docker-compose的 ...

  3. python第四周:装饰器、迭代器、内置方法、数据序列化

    1.装饰器 定义:本质是一个函数,(装饰其他函数)就是为其他函数添加附加功能 原则:不能修改被装饰函数的源代码,不能修改被装饰函数的调用方式 实现装饰器的知识储备: 函数即“变量”.每当定义一个函数时 ...

  4. 《代码敲不队》第八次团队作业:Alpha冲刺 第二天

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 代码敲不队 作业学习目标 掌握软件编码实现的工程要求. 团队项目github仓库地址链接 GitH ...

  5. TOMCAT-IDEA远程debug方法

    在很多情况下,tomcat本地启动并不足以完全模拟线上环境,所以,有时候我们可能需要远程debug方法去调试,下面附上远程idea debug方法: IDEA中,选择 Run/Debug Config ...

  6. Android使用C代码

    Android调用C代码 1.开发工具:Android studio 2.0 2.开发前准备: 2. 3. 4.下面我们就来开发我们的程序吧, [1]创建一个java类 package com.adm ...

  7. nginx与tomcat搭建集群,负载均衡

    --------------------------------------------------- 搭建环境(在桌面上即可完成测试) 先准备2个tomcat服务器 解压tomcat压缩包 得到 把 ...

  8. POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 &amp;&amp; tarjan求ebc &amp;&amp; 缩点构造缩点树】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10798   Accepted: 4626 ...

  9. HDU 4349

    想了好久,没思路.看别人说是卢卡斯,就去看卢卡斯了,看完卢卡斯,再用它推导一下,很容易就知道,答案是2^n的二进制中一的个数.改天找个时间写个卢卡斯的总结.~~~今晚竟然要上形势政治课,靠.... # ...

  10. HDU 4365

    把涂色的格子按对称旋转至左上角. 当未涂色时,若要符合要求,则必须要求每一圈矩形都是上下左右对称的.注意是一圈的小矩形.对于N*N的阵,若最外层一圈的小矩形要符合要求,则(假设N%2==0)可以涂色的 ...