首页 > 其他 > 详细

7.21多校——5289RMQ_st + 二分搜索——Assignment

时间:2015-07-22 15:59:19      阅读:78      评论:0      收藏:0      [点我收藏+]
roblem 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
 

 

Source
 

 

Recommend
We have carefully selected several similar problems for you:  5299 5298 5297 5296 5295 
/*
题意:

RMQ_st算法
复杂度:预处理nlogn 查询O(1)
实现:
用DP的思想
mx[i][j] 定义为以i为起点长度为1<<j的区间内的最大值
状态转移方程 mx[i][j] = mx[i][j-1] + mx[i+(1<<(j-1))][j-1] 
把区间分成长度为1<<(j-1)的两个部分
总共处理的长度从1到log2(n),起点从1到n
所以总复杂度O(nlogn)

二分搜索位置
假定了l到r之间必定有一段
二分搜索从i到n,得到以该点开始能到右边最多多少
如果i到mid可以说明二分区域在另一块,另l = mid
因为有RMQ的存在能得到该区间是否满足,二分对于数列需单调的要求就是因为要使得端点为极值

*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int MAX = 100000 + 10;
int mx[MAX][20], mn[MAX][20];
int a[MAX]; 
int n;
void  rmq()
{
    for(int i = 1; i <= n; i++)
        mx[i][0] = mn[i][0] = a[i];
        int m = log2(n*1.0);
        for(int i = 1; i <= m; i++){
            for(int j = 1;j <= n; j++){
                if(j + (1 << (i - 1)) <= n)
                    mx[j][i] = max(mx[j][i-1], mx[j + (1 << (i - 1))][i - 1]);
                    mn[j][i] = min(mn[j][i-1], mn[j + (1 << (i - 1))][i - 1]);
                }
            }
}

int rmqmin(int l, int r)
{
    int m = log2(1.0*(r - l + 1));
    return min(mn[l][m], mn[r - (1 << m) + 1][m]);
}

int rmqmax(int l, int r)
{
    int m = log2(1.0*(r - l + 1));
    return max(mx[l][m], mx[r - (1 << m) + 1][m]);
}

int cal(int l, int r)
{
    return rmqmax(l, r) - rmqmin(l, r);
}

int main()
{
    int T, k;
    scanf("%d", &T);
    while(T--){
        long long  ans = 0;
        scanf("%d%d", &n, &k);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        rmq();
       // printf("%d %d\n",rmqmax(2, 2),rmqmin(1,5));
        int l, r, mid;
        for(int i = 1; i <= n; i++){
            l = i, r = n;
            while(l + 1 < r){
                 mid = (l + r) >> 1;
                if(cal(i, mid) < k)
                    l = mid;
                else r = mid;
            }
            if(cal(i, r) < k)
                ans = ans + (long long )(r - i + 1);
            else{
               ans = ans + (long long )(l - i + 1);
            }
        }
       printf("%lld\n", ans);
    }
    return 0;
}

            
        

  

7.21多校——5289RMQ_st + 二分搜索——Assignment

原文:http://www.cnblogs.com/zero-begin/p/4667671.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!