hdu 5289 ST表+尺取法

题目:

Assignment

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


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]


分析:                                                                                            



代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100050;
const int inf=INT_MAX;

int t,n,k,a[maxn],st[maxn][16],sti[maxn][16];

inline void initrmq(){
    for(int j=1;(1<<j)<=n;++j){
        for(int i=0;i+(1<<j)-1<n;++i){
            st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);//+比<<优先级高
            sti[i][j]=max(sti[i][j-1],sti[i+(1<<(j-1))][j-1]);
        }
    }
}

inline int rmq(int u,int v){
    int k=(int)(log(v-u+1.0)/log(2.0));
    return max(sti[u][k],sti[v-(1<<k)+1][k])-min(st[u][k],st[v-(1<<k)+1][k]);
}

/*inline void In(int &x){
    char c;x=0;c=getchar();
    int sign=1;
    while(!(c>='0'&&c<='9'||c=='-')) c=getchar();
    if(c=='-') sign=-1,c=getchar();
    while(c>='0'&&c<='9'){
        x=(x<<3)+(x<<1)+c-'0';
        c=getchar();
    }
    x*=sign;
}

inline void Out(__int64 x){
    if(x<0){
        x=-x;
        putchar('-');
    }
    if(x>9) Out(x/10);
    putchar(x%10+'0');
}*/

int main(){///
    __int64 l,r;
    __int64 ans;
    //In(t);
    scanf("%d",&t);
    while(t--){
        //In(n); In(k);
        scanf("%d%d",&n,&k);
        for(int i=0;i<n;++i){
            //In(a[i]);
            scanf("%d",&a[i]);
            st[i][0]=sti[i][0]=a[i];
        }
        initrmq();
        ans=0;
        l=0;///枚举右端点 600ms
        for(int i=0;i<n;++i){
            r=i;
            while(l<r&&rmq(l,r)>=k) ++l;///找到最左的符合条件的左端点
            ans+=(r-l+1);
        }

        /*r=0;///枚举左端点迷之T的写法.....
        for(int i=0;i<n;++i){
            l=i;
            while(r<n)
                while(rmq(l,r)<k) ++r;
            ans+=(r-l);
            //cout<<l<<"  "<<r<<endl;
        }*/
        /*枚举左端点RE的姿势:
        l=0; r=0;
        while(1){
            while(r<n&&rmq(l,r)<k) r++;
            if(r==n) break;
            ans+=r-l;
            l++;
        }
        for(;l<n;++l) ans+=n-l;*/
        printf("%I64d\n",ans);
        //Out(ans);
        //putchar('\n');
    }
    return 0;
}

/*//一个只需要300ms的姿势...
#include<stdio.h>
#include<stdlib.h>

int num[100050],n,k;
__int64 sum;

int main(){
	int i,j,T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&k);
		for(i=0;i<n;i++){
			scanf("%d",&num[i]);
		}
		sum=n;
		int max,min,l;
		max=min=num[0];
		l=0;
		for(i=1;i<n;i++){
			if(num[i]>max)max=num[i];
			else if(num[i]<min)min=num[i];
			if(max-min<k){
				l++;
				sum+=l;
			}
			else{
				min=max=num[i];
				l=0;
				for(j=i-1;abs(num[j]-max)<k&&abs(num[j]-min)<k;j--){
					if(num[j]>max)max=num[j];
					else if(num[j]<min)min=num[j];
					l++;
				}
				sum+=l;
			}
		}
		printf("%I64d\n",sum);
	}
	return 0;
}*/




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值