zoukankan      html  css  js  c++  java
  • HDU 5289 Assignment rmq

    Assignment

    题目连接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5289

    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],…,an,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

    题意

    给你n个数,然后连续的,且这个区间内最大值减去最小值的差小于k的话,就可以算作一队。

    问你一共有多少种分队的方法。

    题解:

    暴力枚举左端点位置,右端点单调递增,用一个rmq维护一下就好了。

    Debug

    mm[0]=-1;未赋初值

    rmq(int x,int y);未般判断x>y的情况。

    j+(1<<(i-1))和 j+1<<(i-1) 不同

    代码

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define N 100050
    ll n,m,val[N],mx[N][20],mi[N][20],mm[N];
    template<typename T>void read(T&x)
    {
        ll k=0; char c=getchar();
        x=0;
        while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
        if (c==EOF)exit(0);
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        x=k?-x:x;
    }
    void read_char(char &c)
    {while(!isalpha(c=getchar())&&c!=EOF);}
    void init_ST(ll n)
    {
        mm[0]=-1;//
        for(ll i=1;i<=n;i++)
        {
            mx[i][0]=mi[i][0]=val[i];
            mm[i]=(i&(i-1))==0?mm[i-1]+1:mm[i-1];
        }
        for(ll i=1;i<=20;i++)
            for(ll j=1;j+(1<<i)-1<=n;j++)
            {
                mx[j][i]=max(mx[j][i-1],mx[j+(1<<(i-1))][i-1]);
                mi[j][i]=min(mi[j][i-1],mi[j+(1<<(i-1))][i-1]);
            }
    }
    ll rmq(ll x,ll y)
    {
        if(x>y)swap(x,y);//
        ll k=mm[y-x+1];
        ll ans=max(mx[x][k],mx[y-(1<<k)+1][k]);
        ans-=min(mi[x][k],mi[y-(1<<k)+1][k]);
        return ans;
    }
    void work()
    {
        ll k;
        read(n); read(k);
        for(ll i=1;i<=n;i++)read(val[i]);
        init_ST(n);
        ll r=0,ans=0;;
        for(ll i=1;i<=n;i++)
        {
            while (r+1<=n&&rmq(i,r+1)<k)r++;
            ans+=r-i+1;
        }
        printf("%lld
    ",ans);
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("aa.in","r",stdin);
    #endif
        ll q;
        read(q);
        while(q--)
        {
            work();
        }
    }
    
    
  • 相关阅读:
    屏蔽2003:在系统启动时至少有一个服务或驱动程序产生错误
    C#中有关string和byte[]转换的问题
    如何控制winform程序只能打开一个呢?
    ArrayList的使用方法【转载】
    ASP.NET中ajaX学习记录
    C#去除字符串空格的几种方法【转载】
    C# 中2,10,16进制及其ASCII码之间转化
    vc++下char数组赋值乱码问题
    c#关机时自动退出程序
    Android入门前言(一)之Android应用开发入门五问 (转自:http://blog.csdn.net/android_tutor/)
  • 原文地址:https://www.cnblogs.com/mmmqqdd/p/10830161.html
Copyright © 2011-2022 走看看