HDU 5289 Assignment [优先队列 贪心]
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
2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9
Sample Output
5
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 [优先队列 贪心]的更多相关文章
- HDU 5289 Assignment(二分+RMQ-ST)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 5289 Assignment rmq
Assignment 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Description Tom owns a company and h ...
- HDU 5289 Assignment (ST算法区间最值+二分)
题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...
- HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is ...
- hdu 5360 Hiking(优先队列+贪心)
题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L, ...
- HDU 5289 Assignment(2015 多校第一场二分 + RMQ)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU - 5289 Assignment (RMQ+二分)(单调队列)
题目链接: Assignment 题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...
- HDU 5289——Assignment——————【RMQ+优化求解】
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
随机推荐
- js提示是否删除
第一种: <a href="javascript:if(confirm('确认删除吗?'))window.location='del.asp'">删除</a> ...
- Echarts堆积柱状图排序问题
Echarts堆积柱状图排序是按照堆积柱状图的柱子高度进行从大到小(或者从小到大)进行排序,方便查阅各坐标情况.以下是我自己研发的方法,有不对的地方敬请谅解,随时欢迎指教. 排序后效果如下图: (1) ...
- BZOJ 1197 [HNOI2006]花仙子的魔法 (数学题)
题面:洛谷传送门 BZOJ传送门 非常有意思的一道数学题,浓浓的$CF$风,然而我并没有想出来.. 我们想把一个$n$维空间用$n$维球分成尽可能多的块 而新增加一个$n$维球时,肯定要尽可能多地切割 ...
- PHP做的简单计算器
使用php做的简易计算器 能够进行+,-,*,/运算. 如下图 <?php if (isset($_POST['button'])) { $num1 = $_POST['num1']; $num ...
- oracle定时器执行一遍就不执行或本就不执行
转:http://blog.csdn.net/qq_23311211/article/details/76283689 以sqlplus/ assysdba进入sql命令模式,使用sql:select ...
- ASP.NET CORE--WIN10上无法单步调试解决方法
参考这篇文章 http://www.cnblogs.com/artech/p/debug-in-vs-code.html In order to be able to debug cross-plat ...
- ubuntu--dpkg 被中断
主要原因应该是/var/lib/dpkg/updates 文件夹里面的资料有错误,使得更新软件的程序出现错误,所以得把它们完全删除,通过sudo apt-get update这个指令会重新建立这些资料 ...
- nodejs-NPM基本使用
搜索模块 npm search express 更新模块 npm update express 卸载模块 npm uninstall express 安装模块 npm install express ...
- 【SSH2框架(理论篇)】--SSH2 Vs 经典三层
这几天一直在学习使用SSH2框架.对于框架本身的使用并非非常困难.相信经过多锻炼就行熟练的掌握框架的使用,让我匪夷所思的是在使用框架的时候感觉非常熟悉,好像在哪里用过似得. 就在某次查看代码的时 ...
- sqlite学习笔记6:更新表数据-update
一 条件推断 在SQL中条件推断使用where,相当于其它变成语言中的if,基本使用方法如: SELECT column1, column2, columnN FROM table_name WHER ...