B - Assignment
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. InputIn 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. OutputFor each test,output the number of groups. Sample Input2 Sample Output5 HintFirst 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> #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("%dn",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,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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |