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 );    }}
阅读全文
0 0
原创粉丝点击