[RMQ+二分] 2015多校联合第一场 Assignment

Assignment

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

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 (n<=100000, 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
First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3]

题意:给定n和k,表示n个数的区间中,求出总共满足条件的区间个数,满足条件的区间为:
1、数字是连续的
2、区间内最大值减区间最小值小于k

枚举每一个左端点,二分该右端点,确定对应的二分位置使每个左端点得到的区间数量最多。

二分搜索的条件:从i到mid中最大值与最小值之差是否小于k。
确定区间范围内的最大值和最小值要用RMQ来做,否则时间不够会超时。。

这还没完,最终的sum值要记得用long long来存,否则会出现溢出的情况。

话说多校联合的比赛是真的很难……很多题目不仅比赛时做不出来,事后看题解也是看得非常费劲。
看起来我的水平还不足以做出多校联合的题目,继续努力。

#include <iostream>
#include <cstdio>
using namespace std;

const int MAXN = 200020;
const int INF = 0x3f3f3f3f;

int a[100010];
int n,k,t;
int dp1[100010][20];
int dp2[100010][20];
int mm[100010];
void initRMQ(int n, int b[]) {
    mm[0] = -1;
    for (int i = 1; i <= n; i++) {
        mm[i] = ((i & (i - 1)) == 0) ? mm[i - 1] + 1 : mm[i - 1];
        dp1[i][0] = b[i];
        dp2[i][0] = b[i];
    }
    for (int j = 1; j <= mm[n]; j++)
        for (int i = 1; i + (1 << j) - 1 <= n; i++) {
            dp1[i][j] = max(dp1[i][j - 1], dp1[i + (1 << (j - 1))][j - 1]);
            dp2[i][j] = min(dp2[i][j - 1], dp2[i + (1 << (j - 1))][j - 1]);
        }
}
int rmq1(int x, int y) {
    int k = mm[y - x + 1];
    return max(dp1[x][k], dp1[y - (1 << k) + 1][k]);
}
int rmq2(int x, int y) {
    int k = mm[y - x + 1];
    return min(dp2[x][k], dp2[y - (1 << k) + 1][k]);
}

int solve(int i)
{
    //如果整段区间都满足区间内最大值减最小值小于k,返回区间长度
    if (rmq1(i,n) -rmq2(i,n) < k) return n-i+1;
    int l = i, r = n; //起点i,终点n,开始二分
    int mid = (l+r) / 2;
    while(l-r<=0)
    {
        if(rmq1(i,mid) - rmq2(i,mid) < k)
            l = mid + 1;
        else
            r = mid - 1;
        mid = (l+r)/2;
    }
    return r - i + 1;

}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        for(int i = 1; i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        long long sum = 0;
        initRMQ(n,a);
        for(int i = 1; i<= n; i++)
            sum += (long long) solve(i);
        printf("%I64d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值