HDU 5289 Assignment [优先队列 贪心]
来源:互联网 发布:淘宝童鞋批发 编辑:程序博客网 时间:2024/06/10 20:21
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-8using 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 5289 Assignment [优先队列 贪心]
- HDU 5289-B - Assignment-贪心
- HDU 4544 贪心+优先队列
- HDU 5360 (贪心 优先队列)
- hdu 1789 贪心 优先队列
- hdu 5360 set+贪心 // 优先队列+贪心
- HDU 5289 Assignment(单调队列)
- hdu 2850(贪心+优先队列)
- Hdu 2850 Load Balancing (贪心 优先队列)
- hdu 2850 Load Balancing (优先队列 + 贪心)
- hdu 5360 Hiking(优先队列+贪心)
- HDU 5360 Hiking (贪心+优先队列)
- hdu 5360 Hiking (优先队列+贪心)
- hdu 5360 Hiking (贪心+优先队列)
- HDU 5360 Hiking (贪心+优先队列)
- hdu 5835 Danganronpa(贪心,优先队列)
- HDU 6000 Wash (优先队列-贪心)
- Hdu 6047 Maximum Sequence【贪心+优先队列】
- 激活函数--activation function 介绍一下几种激活函数的不同
- 机器的日期和时间格式(Instant)
- Linux下进程之间的通信
- 1701H2王建瑜 连续第七天
- 入门ahk7章_进程管理
- HDU 5289 Assignment [优先队列 贪心]
- Spring入门---Spring框架底层原理
- 介绍几种电路上常用的塑料插接件(1)
- 数据库排序查询
- 1682亿!!阿里工程师如何喝着茶创造双11奇迹?
- OgnlContext对象的了解与使用
- 《不负韶华》
- 不重启Linux主机来识别新加的硬盘
- 输入/输出学生信息