zoukankan      html  css  js  c++  java
  • HDU 5289 Assignment(二分+RMQ-ST)

    Assignment

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

    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]

    题目链接:HDU 5289

    题意就是求有多少个连续子串,这些子串均符合相邻数之间差的绝对值均小于k,直接两个for计数估计会T,因此可以枚举子串的左端点$i$,二分右端点$R$,使得$[i,R]$长度最大,那么这样一来这个子串是肯定符合的,实际上把右端点往左缩一个得到的小一个单位的子串也肯定是符合的,这样可以一直缩到区间变成$[i,i]$,因此每一次枚举得到的区间$[i,R]$可以产生$R-i+1$个符合题意的子串。

    另外由于最大答案可以达到$frac{(1+10^5)*10^5} {2}$因此要用long long

    代码:

    #include <stdio.h>
    #include <bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define LC(x) (x<<1)
    #define RC(x) ((x<<1)+1)
    #define MID(x,y) ((x+y)>>1)
    #define CLR(arr,val) memset(arr,val,sizeof(arr))
    #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
    typedef pair<int, int> pii;
    typedef long long LL;
    const double PI = acos(-1.0);
    const int N = 1e5 + 7;
    int arr[N], Max[N][20], Min[N][20];
    
    void rmq_init(int l, int r)
    {
        int i, j;
        for (i = l; i <= r; ++i)
            Max[i][0] = Min[i][0] = arr[i];
        for (j = 1; l + (1 << j) - 1 <= r; ++j)
        {
            for (i = l; i + (1 << j) - 1 <= r; ++i)
            {
                Max[i][j] = max(Max[i][j - 1], Max[i + (1 << (j - 1))][j - 1]);
                Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);
            }
        }
    }
    pii ST(int l, int r)
    {
        int k = log2(r - l + 1);
        int Ma = max(Max[l][k], Max[r - (1 << k) + 1][k]);
        int Mi = min(Min[l][k], Min[r - (1 << k) + 1][k]);
        return pii(Ma, Mi);
    }
    int main(void)
    {
        int tcase;
        scanf("%d", &tcase);
        while (tcase--)
        {
            int n, k, i;
            scanf("%d%d", &n, &k);
            for (i = 1; i <= n; ++i)
                scanf("%d", &arr[i]);
            rmq_init(1, n);
    
            LL ans = 0;
            for (i = 1; i <= n; ++i)
            {
                int L = i, R = n;
                int idx = i;
                while (L <= R)
                {
                    int mid = MID(L, R);
                    pii temp = ST(i, mid);
                    if (temp.first - temp.second < k)
                    {
                        idx = mid;
                        L = mid + 1;
                    }
                    else
                        R = mid - 1;
                }
                ans += (LL)(idx - i + 1);
            }
            printf("%I64d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    经常遇到分析器错误信息: 访问被拒绝 "XXX组件名"
    文本格式检查,读取服务器端文本和客户端文本
    使用bcp,循环将本地txt文本导入远程sqlserver中
    Nginx高并发配置思路(轻松应对1万并发量)
    编译busybox时报错
    centos6.0桌面配置
    我的电脑每次点开的时候窗口都是最大化的怎么办?
    TQ2440上移植linux2.6.25时启动linux中free init 120kB 后卡住
    菜鸟编译Linux内核
    Linux/centos下查看硬件型号
  • 原文地址:https://www.cnblogs.com/Blackops/p/6405100.html
Copyright © 2011-2022 走看看