• HDOJ 题目5289 Assignment(RMQ,技巧)


    Assignment

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


    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
    We have carefully selected several similar problems for you:  5315 5314 5313 5312 5311 
     题目大意:输入n,k,问n个数的序列。有多少个区间,最大值减最小值小于k
    暴力找区间超时一次
    ac代码,跑了1000ms
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #define max(a,b) (a>b?a:b)
    #define min(a,b) (a>b?b:a)
    int n;
    __int64 m;
    __int64 a[100010];
    __int64 maxv[100010][20],minv[100010][20];
    void init()
    {
    	int i,j,k;
    	for(i=1;i<=n;i++)
    	{
    		minv[i][0]=a[i];
    		maxv[i][0]=a[i];
    	}
    	for(j=1;(1<<j)<=n;j++)
    	{
    		for(k=0;k+(1<<j)-1<=n;k++)
    		{
    			minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);
    			maxv[k][j]=max(maxv[k][j-1],maxv[k+(1<<(j-1))][j-1]);
    		}
    	}
    }
    __int64 q_max(int l,int r)
    {
    	int k=(int)(log((double)(r-l+1))/log(2.0));
    	return max(maxv[l][k],maxv[r-(1<<k)+1][k]);
    }
    __int64 q_min(int l,int r)
    {
    	int k=(int)(log((double)(r-l+1))/log(2.0));
    	return min(minv[l][k],minv[r-(1<<k)+1][k]);
    }
    int main()
    {
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d%I64d",&n,&m);
    		int i,j,k;
    		for(i=1;i<=n;i++)
    		{
    			scanf("%I64d",&a[i]);
    		}
    		init();
    		__int64 ans=0;
    		int p=1;
    		/*for(i=1;i<=n;i++)//暴力超时
    		{
    			for(j=i;j<=n;j++)
    			{
    				if(q_max(i,j)-q_min(i,j)<m)
    					ans++;
    			}
    		}*/
    		for(i=1;i<=n;i++)
    		{
    			while(q_max(p,i)-q_min(p,i)>=m&&p<i)
    			{
    				p++;
    			}
    			ans+=i-p+1;
    		}
    		printf("%I64d
    ",ans);
    	}
    }

    网上看到用deque做的,跑了577ms,收藏一下,http://www.bubuko.com/infodetail-987302.html
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std ;
    #define LL __int64
    deque <LL> deq1 , deq2 ;
    //单调队列,deq1最大值,deq2最小值
    LL a[100010] ;
    int main() {
        int t , n , i , j ;
        LL k , ans ;
        scanf("%d", &t) ;
        while( t-- ) {
            scanf("%d %I64d", &n, &k) ;
            for(i = 0 ; i < n ; i++)
                scanf("%I64d", &a[i]) ;
            if(k == 0) {
                printf("0
    ") ;
                continue ;
            }
            while( !deq1.empty() ) deq1.pop_back() ;
            while( !deq2.empty() ) deq2.pop_back() ;
            for(i = 0 , j = 0 , ans = 0; i < n ; i++) {//i在前。j在后
                while( !deq1.empty() && deq1.back() < a[i] ) deq1.pop_back() ;
                deq1.push_back(a[i]) ;
                while( !deq2.empty() && deq2.back() > a[i] ) deq2.pop_back() ;
                deq2.push_back(a[i]) ;
                while( !deq1.empty() && !deq2.empty() && deq1.front() - deq2.front() >= k ) {
                    ans += (i-j) ;
                    //printf("%d %d,%I64d %I64d
    ", i , j, deq1.front() , deq2.front() ) ;
                    if( deq1.front() == a[j] ) deq1.pop_front() ;
                    if( deq2.front() == a[j] ) deq2.pop_front() ;
                    j++ ;
                }
            }
            while( j < n ) {
                ans += (i-j) ;
                j++ ;
            }
            printf("%I64d
    ", ans) ;
        }
        return 0 ;
    }


  • 相关阅读:
    动态网站项目(Dynamic Web Project)CRUD(增删改查)功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8),前端使用JSP+JSTL+EL组合
    动态网站项目(Dynamic Web Project)登录功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8)(js验证+cookie)
    Oracle数据库scott用户无法导入数据的解决方法
    Win10安装Oracle 11g后解决sqldeveloper缺少快捷方式的问题
    【转载】win10系统安装oracle11g详细步骤
    WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决
    WPF学习笔记(7):DataGrid中数字自定义格式显示
    WPF学习笔记(6):DataSet更新后台数据库个别列失败的问题
    WPF学习笔记(5):两个DataGrid的滚动条实现同步滚动
    WPF学习笔记(4):获取DataGridTemplateColumn模板定义的内容控件
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7017677.html
Copyright © 2020-2023  润新知