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 ...
随机推荐
- mmap,malloc分配随机内存
随机数1G #cat malloc_rand_1g.c #include <stdio.h> /* printf, scanf, NULL */ #include <stdlib.h ...
- mongodb 和 mongoose 初探
mongodb MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 1. 安装相关 1.1 下载 官网下载地址 :官网下载社区版 1.2 安 ...
- 记Spring搭建功能完整的个人博客「Oyster」全过程[其一] 整体思路:需求、架构及技术要求
一两个星期前正在了解Linux内核,看得有点累,突然想趁着五一放假写个博客学学spring. 由于没有在一开始下定决心写这个博客系统,所以我又没记录一开始的分析过程.这都是写了一个星期之后的思路了. ...
- HDU1527 - 取石子游戏【威佐夫博弈】
有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者为胜者. ...
- [Noi2002]Savage
[Noi2002]Savage 数学题. 题解回去写(有个坑点) flag++ #include <cstdio> int n,m,c[25],p[29],l[29]; int exgcd ...
- AES ECB PKCS5/PKCS7 加解密 python实现 支持中文
目录 ECB模式介绍 pkcs5padding和pkcs7padding的区别 python实现 注意事项 ECB模式介绍 电码本模式(Electronic Codebook Book (ECB) 这 ...
- Github+Jekyll 搭建个人网站详细教程
GitHub搭建个人网站,大家在网上一搜能搜到一大把的教程,但是大部分都讲的差不多,并不能满足自己想搭建的网站详细需求.我之前在搭建本站的时候也是查了较多资料,学习了下jekyll语法,参考了几个主题 ...
- VS2013找不到SDKDDKVer.h
今天在升级vs2010 的project的时候遇到了一个这种问题.提示:找不到SDKDKVer.h 通过查找资料发现,原来是vs版本号之间Windows SDK的路径宏定义不同,有些坑. 网上有人说能 ...
- wifi破解不是真黑客不靠谱?
Wifi破解神器骗局:摆地摊+网络兜售 近日,"万能wifipassword破解器"风靡全国地摊.各地小贩開始兜售这样的蹭网卡.声称可破解各种wifipassword,当场測试也是 ...
- DatabaseMetaData开发实务
1.总论 在企业开发实务中,数据迁移是经常会遇到的事情,此时,需要搞清楚,源数据库与目的数据库之间表以及表内部各列之间的异同.而有些时候,我们拿到的项目文 档,未必能准确表述各个表的准确结构,即使应用 ...