HDU 5289 Assignment

Posted HAZELNUT MUSEUM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 

4 2 
3 1 2 4 
10 5 
0 3 4 5 2 1 6 7 8 9 
Sample Output 

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 5289 Assignment的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5289 Assignment

HDU - 5289 Assignment (RMQ+二分)

HDU 5289 Assignment(多校联合第一场1002)

HDU 5289 Assignment(二分+RMQ-ST)

HDU 5289 Assignment(单调队列)

hdu5289 Assignment (区间查询最大值最小值,st算法...)