Assignment
Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 297 Accepted Submission (s): 152
Problem Descriptiontom owns a company and he's the boss. There is n staffs which is numbered from 1 to N in the this company, and every the staff have a ability. Now, Tom was going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability for any of the. 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 have n Persons, k means the maximum difference between abilities of the staff in a group are 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.
Outputfor each Test,output the number of groups.
Sample Input24 23 1 2 410 50 3 4 5 2 1 6 7 8 9
Sample Output528
HintFirst Sample, the satisfied groups include:[1,1], [2,2], [3,3], [bis], [2,3]
Source2015 multi-university Training Contest 1 Test instructions: There are n individuals to group, each group of the number of people to be continuous and any two groups of each group of the difference is less than K, the number of groups to meet the conditions. Analysis: Maintain the maximum value with a monotonic queue. You can use Fi to represent the number of intervals that satisfy the condition at the end of the point I, resulting in ans=sum{fi}. The number of intervals in the interval [i,j] is =j-i+1; it is necessary to maintain a queue of maximum and minimum values to calculate the range of non-conforming conditions, and then subtract the interval that does not meet the criteria.
#include <cstdio>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<stdlib.h>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn=100000+5;intA[MAXN],Q1[MAXN],Q2[MAXN];intkase,n,k;intMain () {CIN>>Kase; while(kase--) {LL ans=0; intF1,f2,t1,t2,l1,l2; F1=f2=t1=t2=0; L1=l2=-1; scanf ("%d%d",&n,&k); for(intI=0; i<n;i++) scanf ("%d",&A[i]); for(intI=0; i<n;i++) { while(F1<t1 && a[q1[t1-1]]<=a[i]) t1--;//Maintain maximum queue (decrement)q1[t1++]=i; while(F2<t2 && a[q2[t2-1]]>=a[i]) t2--;//Maintain minimum queue (increment)q2[t2++]=i; while(A[q1[f1]]-a[q2[f2]]>=k)//The difference is too largeQ1[F1]<Q2[F2]? l1=q1[f1++]: l2=q2[f2++]; Ans+=i-Max (L1,L2); } printf ("%lld\n", ans); } return 0;}View Code
HDU 5289 Assignment (monotone queue)