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
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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值