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;
    }
  • 相关阅读:
    关于slot标签的使用
    vue组件
    template和component的理解(待更正)
    input 的属性autocomplete
    关于render: h => h(App)和components: { App }的区别
    关于vue的简单知识
    获取页面跳转携带的参数
    关于跨域的简单想法(此想法是错误的,特此备注)
    关于jq建立类似与双向绑定的函数
    关于jq将一个页面引入另一个页面,类似与组件化的解决方案
  • 原文地址:https://www.cnblogs.com/zxhl/p/5271687.html
Copyright © 2011-2022 走看看