2015 Multi-University Training Contest 1 - Assignment - (单调队列)

Assignment

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
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] [/hint]
 

Author
FZUACM
 

Source
2015 Multi-University Training Contest 1

题意:给出n、k和长度为n的序列a[1]~a[n],现在有两两分组规则:每组中元素必须是连续的而且组内元素的最大值与最小值之差不能超过k。问能有多少个不同的组。

解析:对于每个起点i,问题在于找到其最靠右的终点j,使得i~j之内最大值与最小值之差不超过k,那么以j为起点的序列就只能从[i,j]选一个终点,有j-i+1种选法。找终点j时就要用到单调队列,主要通过遍历终点j,用单调队列来更新目前的最大与最小值,当最大与最小值差值超过k时,更新起点i。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxN=100000+5;

int n,k;
int a[maxN];
ll ans;
deque<int> low; //双端队列
deque<int> high;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);

        low.clear();
        high.clear();
        ans=0;

        int i,j;    //i为起点,j为终点
        //枚举的是终点j,但实际上计算的是每个起点i对应的group数量
        for(i=j=1;j<=n;j++)
        {
            while(!low.empty()&&low.back()>a[j]) low.pop_back();    //low是一个到j为止递增的序列
            low.push_back(a[j]);
            while(!high.empty()&&high.back()<a[j]) high.pop_back(); //high是一个到j为止递减的序列
            high.push_back(a[j]);

            while(!low.empty()&&!high.empty()&&(high.front()-low.front())>=k)
            {
                ans+=j-i;
                //现在要更换起点i,所以如果起点正好是两个队列的队首,则弹出队首
                if(low.front()==a[i]) low.pop_front();
                if(high.front()==a[i]) high.pop_front();
                i++;
            }
        }
        //剩余的区间[i,n]中任意两个元素均满足条件
        while(i<=n)
        {
            ans+=j-i;
            i++;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值