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 ...
随机推荐
- jmeter实现多并发
1.jmeter实现多并发 线程组:负载发生器,用以多线程或多进程的方式来模拟用户的使用行为.jmeter是以线程的方式来进行模拟用户的并发访问的
- Project Euler 28 Number spiral diagonals
题意:给出一个 1001 × 1001 的矩阵,寻找每一圈四个顶点,并求出所有顶点的和 思路:只需要找到右上顶点数字的规律,然后每一圈四个顶点构成了一个等差数列,求个和即可 /************ ...
- 一、简介 ELO商户类别推荐有助于了解客户忠诚度
Elo Merchant Category Recommendation Help understand customer loyalty (ELO商户类别推荐有助于了解客户忠诚度) 竞赛描述: 想象 ...
- BA--三相异步电机_星三角降压启动
星一三角启动就是一种简单方便的降压启动方式,同时还可通过手动和自动操作控制方式实现. 星三角起动的优点还是很显著的,因为同任何别的减压起动器相比较,其结构最简单,价格也最便宜 除此之外,星三角起动方式 ...
- valueof这个万能方法,将string转换为int或者int转换为string都可以
private static String testString = "111"; int stringInt = Integer.valueOf(testString); Str ...
- [SharePoint][SharePoint Designer 入门经典]Chapter13 客户端Silverlight编程
1.使用Silverlight,CAML和Linq取得数据 2.编程性创建更新删除列表数据项 3.修饰列表和库的配置 4.管理文件和文件夹 5.修改快速启动和顶部导航条 [使用Silverlight, ...
- 【算法拾遗(java描写叙述)】--- 选择排序(直接选择排序、堆排序)
选择排序的基本思想 每一趟从待排序的记录中选出关键字最小的记录,顺序放在已排好序的子文件的最后,知道所有记录排序完毕.主要有两种选择排序方法:直接选择排序(或称简单选择排序)和堆排序. 直接选择排序 ...
- 经验之谈—OAuth授权流程图
事实上我们在开发中,常常须要解决获得用户的一些特定的数据,比方:能够选择使用微博登陆.使用QQ登陆等等.然后我们间接的获得用户的头像.昵称等信息.这些都涉及到OAuth授权的内容 OAuth授权有这么 ...
- CSDN高校俱乐部第二届战神杯第二题题解
两个人玩一个数字游戏,给定两个正整数A,B,两个人轮流从一个数中减去另外一个数的正数倍.要保证结果非负, 首先得到0的人获胜. 比如:30 8经过一步操作能够变为22 8 或者14 8 或者 6 8. ...
- Error: CompareBaseObjectsInternal can only be called from the main thread
Posted: 01:39 PM 06-17-2013 hi, we're working on a project where we need to do some calculations on ...