zoukankan      html  css  js  c++  java
  • 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
    ",ans-1);
        }
    }
     
  • 相关阅读:
    Linux下python2.7安装pip
    图片转tfrecords
    Python+argparse+notebook
    Pycharm缺少环境变量+无法获取libcudnn.so.6
    Python手动实现k-means
    手动实现二值化
    Tensorflow+InternalError: Blas GEMM launch failed
    tensorflow-gpu+"Failed to create session"
    Ubuntu16.04+wineQQ+解决版本过低
    /usr/lib/nvidia-384/libEGL.so.1 is not a symbolic link
  • 原文地址:https://www.cnblogs.com/HazelNut/p/7821072.html
Copyright © 2011-2022 走看看