zoukankan      html  css  js  c++  java
  • B

    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],…,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]

    问有多少个团体,团体要求能力最大和最小的差距不超过k,rmq,因为区间越长,最大值越大最小值越小,所以可以用二分找到左端点固定,右端点最右的区间,就可以了

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include <iomanip>
    #include<cmath>
    #include<float.h> 
    #include<string.h>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #define sf scanf
    #define pf printf
    #define scf(x) scanf("%d",&x)
    #define scff(x,y) scanf("%d%d",&x,&y)
    #define prf(x) printf("%d
    ",x) 
    #define mm(x,b) memset((x),(b),sizeof(x))
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=a;i>=n;i--)
    typedef long long ll;
    const ll mod=1e9+7;
    const double eps=1e-8;
    const int inf=0x3f3f3f3f;
    using namespace std;
    const double pi=acos(-1.0);
    const int N=1e5+10;
    int a[N];
    int dpmax[N][20],dpmin[N][20];
    void first(int n)
    {
    	mm(dpmax,0);
    	mm(dpmin,0);
    	rep(i,1,n+1)
    	{
    		dpmin[i][0]=dpmax[i][0]=a[i];
    	}
    	for(int j=1;(1<<j)<=n;j++)
    	{
    		for(int i=1;i+(1<<j)-1<=n;i++)
    		{
    			dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
    			dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
    		}
    	}
    }
    int fmax(int l,int r)
    {
    	int x=0;
    	while(l-1+(1<<x)<=r) x++;
    	x--;
    	return max(dpmax[l][x],dpmax[r-(1<<x)+1][x]);
    }
    int fmin(int l,int r)
    {
    	int x=0;
    	while(l-1+(1<<x)<=r) x++;
    	x--;
    	return min(dpmin[l][x],dpmin[r-(1<<x)+1][x]);
    }
    bool judge(int l,int mid,int k)
    {
    	return fmax(l,mid)-fmin(l,mid)<k;
    }
    int main()
    {
    	int re;scf(re);
    	while(re--)
    	{
    		
    		int n,k;scff(n,k);
    		rep(i,1,n+1) scf(a[i]);
    		first(n);
    		ll ans=0;
    	rep(i,1,n)
    	{
    		int l=i,r=n,mid;
    		while(r-l>1)
    		{
    			mid=(l+r)/2;
    			if(judge(i,mid,k))
    			l=mid;
    			else
    			r=mid;
    		}
    		while(judge(i,l,k)&&l<=n) 
    		l++;
    		l--;
    		ans+=l-i+1;
    	}
    		ans++;
    		pf("%lld
    ",ans);
    	}
    	
    }
    

    还有一种用deque的方法 ,速度更快,慢慢体会吧...

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include <iomanip>
    #include<cmath>
    #include<float.h> 
    #include<string.h>
    #include<algorithm>
    #define sf scanf
    #define pf printf
    #define scf(x) scanf("%d",&x)
    #define scff(x,y) scanf("%d%d",&x,&y)
    #define prf(x) printf("%d
    ",x) 
    #define mm(x,b) memset((x),(b),sizeof(x))
    #include<vector>
    #include<queue>
    #include<deque>
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=a;i>=n;i--)
    typedef long long ll;
    const ll mod=1e9+7;
    const double eps=1e-8;
    const int inf=0x3f3f3f3f;
    using namespace std;
    const double pi=acos(-1.0);
    const int N=1e5+10;
    int a[N];
    deque<int>v1;
    deque<int>v2;
    int main()
    {
    	int re,n,k;
    	scf(re);
    	while(re--)
    	{
    		ll ans=0;
    		scff(n,k);
    		rep(i,1,n+1)
    			scf(a[i]);
    		v1.clear();
    		v2.clear();
    		int i,j;
    		for( i=1,j=1;i<=n;i++)
    		{
    			while(!v1.empty()&&v1.back()<a[i]) v1.pop_back();
    			v1.push_back(a[i]);
    			while(!v2.empty()&&v2.back()>a[i]) v2.pop_back();
    			v2.push_back(a[i]);
    			while(!v1.empty() &&!v2.empty() &&(v1.front()-v2.front()>=k))
    			{
    				ans+=i-j;
    				if(v1.front()==a[j]) v1.pop_front();
    				if(v2.front()==a[j]) v2.pop_front();
    				j++;
     			}
    		}
    		while(j<=n)
    		{
    			ans+=i-j;
    			j++;
    		}
    		cout<<ans<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    时空地图TimeGIS 可编辑ArcGIS的Shape矢量文件的地理信息系统
    快手 KSCAD 5.0 矢量图形设计软件
    DWG2SHP DXF2SHP 如何把AutoCAD的DWG,DXF文件转换为Esri ArcGIS的Shape文件
    火了网址
    GIS 网站参考
    快手4.0 (KSCAD)
    用词法分析器Flex过滤日志
    编译器工具 Flex Bison for Windows 简单入门例子
    针对于网络安全领域中基于PCAP流量的数据集
    RBAC权限框架_MVC权限框架
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/9577889.html
Copyright © 2011-2022 走看看