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

    Assignment

    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]
     
    Author
    FZUACM
     
    题意:
    给你一个长度为n,的序列和一个k,让你找出有多少个[l,r]区间满足: 该区间的最大最小值之差小于k
    题解:
    我们固定左端点l,二分其右端点,用RMQ维护最大最小值就好了
     
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 1e5+20, M = 30005, mod = 1000000007, inf = 0x3f3f3f3f;
    typedef long long ll;
    //不同为1,相同为0
    int dp[N][22],f[N][22],a[N],n,T,k;
    void RMQ() {
        for(int i = 1; i <= n; i++) dp[i][0] = f[i][0] = a[i];
        for(int i = 1; (1<<i) <= n; i++) {
            for(int j = 1; (j + (1<<i) - 1) <= n; j++) {
                dp[j][i] = max(dp[j][i-1],dp[j + (1<<(i-1))][i-1]);
                f[j][i] = min(f[j][i-1],f[j + (1<<(i-1))][i-1]);
            }
        }
    }
    int query(int l,int r,int p) {
        if(l == r) return a[l];
        int k = (int) ((log((double)(r - l + 1))) / log(2.0));
        if(p) return max(dp[l][k] , dp[r - (1<<k) + 1][k]);
        else return min(f[l][k] , f[r - (1<<k) + 1][k]);
    }
    int main() {
      scanf("%d",&T);
      while(T--) {
    
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        RMQ();
        ll ans = 0 ;
        for(int l=1;l<=n;l++) {
            int L = l, R = n,A=l;
            while(L<=R) {
                int mid = (L+R)>>1;
                if(query(l,mid,1)-query(l,mid,0)<k) L = mid+1,A=mid;
                else  R = mid-1;
            }
            ll sum = A-l+1;
            ans+=(sum);
        }
       printf("%I64d
    ",ans);
      }
      return 0;
    }
  • 相关阅读:
    stack, deque 和 queue的对比
    Android 修改圆形progressBar颜色
    java.lang.OutOfMemoryError: Failed to allocate a 3110419 byte allocation with 741152 free bytes and
    Android GreenDAO 3.0 不修改版本号的情况下增加、删除表、添加字段
    Android监听安装卸载
    Android实现异步的几种方法
    Android GreenDao清空数据库的方法
    Android 6.0 动态权限申请
    Android无需权限显示悬浮窗
    Android 极光推送JPush---自定义提示音
  • 原文地址:https://www.cnblogs.com/zxhl/p/5271687.html
Copyright © 2011-2022 走看看