zoukankan      html  css  js  c++  java
  • HDU 5289 Assignment(单调队列)

    Assignment

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 297    Accepted Submission(s): 152


    Problem Description
    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:a[1],a[2],…,a[n](0<=a[i]<=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]
     
    Source
     
     
    题意:有N个人要分组,每个组的人的编号要连续而且每组任意两人的差值要小于k,求满足条件的分组有多少个。
    分析:用单调队列维护最值。
    可以用Fi来表示以第i个点结尾的满足条件的区间的个数,结果ans=sum{Fi}。
    区间[i,j]中的区间个数=j-i+1;
    那么就要维护一个最大值和最小值的队列来计算出不符合条件的区间,然后减去不符合条件的区间。
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=100000+5;
    int a[MAXN],q1[MAXN],q2[MAXN];
    int kase,n,k;
    int main()
    {
        cin>>kase;
        while(kase--)
        {
            LL ans=0;
            int f1,f2,t1,t2,l1,l2;
            f1=f2=t1=t2=0;
            l1=l2=-1;
            scanf("%d %d",&n,&k);
            for(int i=0;i<n;i++) scanf("%d",&a[i]);
            for(int i=0;i<n;i++)
            {
                while(f1<t1 && a[q1[t1-1]]<=a[i]) t1--;//维护最大值队列(递减)
                q1[t1++]=i;
                while(f2<t2 && a[q2[t2-1]]>=a[i]) t2--;//维护最小值队列(递增)
                q2[t2++]=i;
                while(a[q1[f1]] - a[q2[f2]]>=k)//差值过大
                    q1[f1]<q2[f2] ? l1=q1[f1++] : l2=q2[f2++];
                ans+=i-max(l1,l2);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Golang Gin使用模板及框架下返回Json
    Golang Web下返回HTML内容处理方法
    Golang net.http和Gin框架下的模板--View调用后台方法 --view接收后台数据的运用 及 嵌套模板和继承模板的使用
    设置oracle编辑的快捷方式
    oracle中的异常处理方法
    游标的使用
    网站登录界面包数据库异常
    在PLSQL中不能使用中文作为查询条件查询数据
    oracle错误一览表
    oracle导入时报错
  • 原文地址:https://www.cnblogs.com/clliff/p/4665756.html
Copyright © 2011-2022 走看看