HDU 5289 Assignment [优先队列 贪心]
2017-11-12 10:00
323 查看
HDU 5289 - Assignment
http://acm.hdu.edu.cn/showproblem.php?pid=5289Tom 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+7; 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=0;i<n;i++) scanf("%d",&arr[i]); ll ans = 1; int l = 0, MIN = INF, MAX = -INF;//, pmi = 0, pma = 0; for(int i=0;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 + 1); low.pop(); MIN = low.top().val; } } else { while(!high.empty() && MAX - arr[i] >= k) { l = max(high.top().pos + 1,l); high.pop(); MAX = high.top().val; } } } ans += i-l+1; } printf("%lld\n",ans-1); } }
相关文章推荐
- HDU 6000 Wash【优先队列优化贪心】【最大值+最小值】
- HDU 5289 Assignment(单调队列)
- Hdu 5289-Assignment 贪心,ST表
- hdu 2850 Load Balancing (优先队列 + 贪心)
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
- 【HDU】5859 Captain is coding【优先队列贪心】
- HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)
- hdu 5289 ST表+双指针或者优先队列或者multiset
- hdu 4544 湫湫系列故事——消灭兔子 贪心+优先队列 解题报告
- HDU 5289 Assignment(多校2015 RMQ 单调(双端)队列)
- HDU 5289 Assignment(单调队列)
- Hdu 6047 Maximum Sequence【贪心+优先队列】
- hdu 5242 正解树链剖分,我的优先队列贪心
- HDU 5360——Hiking——————【贪心+优先队列】
- HDU 6000 Wash (优先队列-贪心)
- 【多校第一场】【单调队列】HDU 5289 Assignment
- hdu 5289 - Assignment(2015 Multi-University Training Contest 1 )单调队列+RMQ+树状数组
- 贪心_简单直接贪心[优先队列](HDU_1009)
- HDU 5289 Assignment(单调队列)
- HDU 5360 【优先队列+贪心】