hdu 5291 Assignment 2015 Multi-University Training Contest 1



Assignment

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


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
 

Source
 

Recommend
 



题目等价于求n个端点分别问1,2,3...,n且满足题意的最长区间。

二分+RMQ
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,l,mid
#define rson    num<<1|1,mid+1,r
#define MID   int mid=(l+r)>>1

using namespace std;
const int maxn=  100000+20  ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();


int n,k;
int a[maxn];
int MAX[maxn][20];
int MIN[maxn][20];
int lll;
inline void initial()
{
    for(int j=1;j<=n;j++ )  MIN[j][0]=MAX[j][0]=a[j];
    for(int i=1;(1<<i)<=n ;i++)
    {
        for(int j=1;j+(1<<i)-1<=n;j++)
        {
            MIN[j][i]=min(MIN[j][i-1],MIN[j+(1<<(i-1))][i-1] );
        MAX[j][i]=max(MAX[j][i-1],MAX[j+(1<<(i-1))][i-1] );
        }
    }
}


int  querymax(int l,int r)
{
    int k=(int)  (log(  double(r-l+1)  )/log(2.0));

//    return max(   MAX[l][k],MAX[l+  (1<<k)][k]      ); 不能这样写是因为会出界,即l+  (1<<k)可能大于r
      return max(MAX[l][k]   ,MAX[r-(1<<k)+1][k]);

}
int  querymin(int l,int r)
{

    int k=(int)  (log(  double(r-l+1)  )/log(2.0) );

 //   return min(   MAX[l][k],MAX[l+  (1<<k)][k]      );不能这样写是因为会出界,即l+  (1<<k)可能大于r
      return min(MIN[l][k]   ,MIN[r-(1<<k)+1][k]);

}

int  binary(int &l   ,int r  ,int i    )  //这个二分值得体会,
{
    int mid;
    while(l<=r)
    {
         mid=(l+r)/2;
        int rmax=querymax( mid,i  );//i是固定的
        int rmin=querymin( mid,i );
        int delta=rmax-rmin;
        if( delta<k   )   r=mid-1;      //如果mid是最终的正确答案,现在若把mid筛掉了,之后的循环会通过"l=mid+1"最终回到现在的mid,然后不满足循环条件,跳出,答案就是mid;
        else        l=mid+1;

    }


    return i-l+1;   /*答案:对于1个i,i在区间右端,其满足条件的最长区间的最左端位置
    保留l的原因是:答案总是靠右边的,
    如果r-l==2,一定会到r-l==0的情况;
    如果r-l==1,那么如果l=mid+1,会到r-l==0的情况,
                    如果r=mid-1,已经证明此时l是正确(因为mid等于l)那么结束循环,答案就是最多到l;
    如果r-l==0,那么若r=mid-1,证明l和r可取,之后跳出循,答案为l
                    若l=mid+1,证明该区域无答案,答案就是l+1或者r+1;




                     */
}
void check()
{
        for(int i=0;(1<<i)<=n ;i++)
    {
        for(int j=1;j+(1<<i)-1<=n;j++)
        {
            cout<<j<<" "<<i<<" "<<MAX[j][i]<<" "<<MIN[j][i]<<endl;
        }
    }
}
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
     int T;
     scanf("%d",&T);
     while(T--)
     {
         scanf("%d%d",&n,&k);
         FOR1(i,n)  scanf("%d",&a[i]);
         initial();
//         check();//correct
          lll=1;
          ll ans=0;

         for(int i=1;i<=n;i++)
            ans+=binary( lll,i,i)/*,cout<<i-binary( lll,i,i)+1<<endl*/;
//            cout<<binary(  lll,4,4)<<endl;

         printf("%I64d\n",ans);

     }



    return 0;
}

/*
2
10 5
0 3 4 5 2 1 6 7 8 9
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值