hdu 5289 ST表+双指针或者优先队列或者multiset
来源:互联网 发布:软件过程管理 pdf 编辑:程序博客网 时间:2024/06/05 20:05
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
Hint
First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]
题意:问数组a中,区间[i,j]中最大值和最小值之差不超过k,问这样的区间有多少个。
好几种写法:原理一样 找到区间的最小最大值,因为是连续的,所以把前面不符合的情况去掉就好。
解法1:st+贪心+双指针。
#include <bits/stdc++.h>using namespace std;int a[200005];int mx[200005][30];int mn[200005][30];int n;int num;int k;void init(){ for(int i=1;i<=n;i++) mn[i][0]=mx[i][0]=a[i]; for(int j = 1; (1<<j) <= n; ++j) for(int i = 1; i + (1<<j) - 1 <= n; ++i) { mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]); mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]); } }int rmq(int i,int j){ int k = 0; while((1 << (k + 1)) <= j - i + 1) k++; int maxx=max(mx[i][k],mx[j-(1<<k)+1][k]); int minn=min(mn[i][k],mn[j-(1<<k)+1][k]); return maxx-minn;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d",&a[i]); init(); int h=1; long long sum=0; for(int i=1;i<=n;i++) { while(rmq(h,i)>=k&&h<i) h++;//把不行的排除掉 剩下的可以随意搭配 sum+=(i-h+1);//以i结束的组的数量 例如 i=5,h=3,就是3,(5~5),(5~4),(5~3). } printf("%lld\n",sum ); }}
解法2:multiset 维护,因为multiset 自动维护区间最大最小值,所以只有双指针不断排除就好,但是没有st好,st更快。。
#include <bits/stdc++.h>using namespace std;multiset<int> minn;multiset<int,greater<int> > maxn;int a[100005];int main(){ int T; scanf("%d",&T); while(T--) { int n,k; scanf("%d%d",&n,&k); maxn.clear(); minn.clear(); long long ans=0; int j=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); maxn.insert(a[i]); minn.insert(a[i]); multiset<int>::iterator it1,it2; while(j<=i&&*maxn.begin()-*minn.begin()>=k) {//j指针一直删除数据,直到后面的再次满足。相当于实现了线段树的删除操作 it1=maxn.find(a[j]); it2=minn.find(a[j]); maxn.erase(it1); minn.erase(it2); j++; } ans+=maxn.size(); } printf("%lld\n",ans ); }}
- hdu 5289 ST表+双指针或者优先队列或者multiset
- 【HDU 3785】寻找大富翁(sort或者优先队列-水题)
- HDU 5289 单调队列,multiset
- hdu 5289 Assignment(ST表+双指针)
- HDU 4302 Holedox Eating(线段树/优先队列/multiset)
- hdu 4453 Looploop(Splay或者三个双端队列)
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
- BUAA 702水题-哈夫曼(小顶堆或者优先队列解决)
- Moo University - Financial Aid (poj 2010 优先队列 或者 二分)
- 2006: [NOI2010]超级钢琴 ST表+优先队列
- bzoj 2006: [NOI2010]超级钢琴 (st表+优先队列)
- [2017纪中10-21]Fantasy 优先队列+ST表
- POJ 1521 Entropy 优先队列/multiset
- Leetcode|Sliding Window Maximum(multiset,优先队列,双端队列和区间树的应用)
- HDU 5098 Smart Software Installer 双队列拓扑排序或者DAG的最长路
- hdu 4123 Bob’s Race(单调队列或者rmq)
- hdu 5289 Assignment(RMQ,单调队列,multiset)
- 二分 或者 指针
- linux shell判断文件,目录是否存在或者具有权限
- 北大“人工智能前沿与产业趋势”课程整理02
- C++11之std::function和std::bind
- 基于信号处理的在线云评测+社区系统( 5)
- 八、反射
- hdu 5289 ST表+双指针或者优先队列或者multiset
- JavaScript中运算符的优先级汇总
- Shell使用总结(一)
- Struts2中default.properties常量默认配置文件详解,以及修改常量方式
- 二叉树的广度与深度遍历
- Springmvc中@RequestParam传值中文乱码解决方案
- Oracle基础查询语句80条
- 使用java底层实现邮件的发送(含测试,源码)
- java 学生成绩管理系统