Title: Http://acm.hdu.edu.cn/showproblem.php?pid=5289Assignment
Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 3175 Accepted Submission (s): 1457
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]
Authorfzuacm
Source2015 multi-university Training Contest 1
Recommendwe has carefully selected several similar problems for you:5669 5668 5667 5666 5665 Test instructions: give you a sequence, give you a K, ask how many consecutive The sub-sequence makes the maximum and minimum difference between the subsequence values
less than K. Problem: Greedy +st table enumerates the right endpoint, because the left endpoint must be incremented or invariant, so the maximum minimum difference in the interval that encounters an enumeration is greater than or equal to K, and the left endpoint is added 1. Then the number of each statistic can be. Note: The answer is to open a long long.
1#include <bits/stdc++.h>2 using namespacestd;3 #defineMAXN 1000104 intn,mn[maxn][ -],mx[maxn][ -],A[MAXN];5 intRead ()6 {7 ints=0, fh=1;CharCh=GetChar ();8 while(ch<'0'|| Ch>'9'){if(ch=='-') fh=-1; ch=GetChar ();}9 while(ch>='0'&&ch<='9') {s=s*Ten+ (ch-'0'); ch=GetChar ();}Ten returns*fh; One } A voidST () - { - inti,j; the for(i=1; i<=n;i++) mn[i][0]=mx[i][0]=A[i]; - for(j=1;(1<<J) <=n;j++) - { - for(i=1; i+ (1<<J)-1<=n;i++) + { -Mn[i][j]=min (mn[i][j-1],mn[i+ (1<< (J-1))][j-1]); +Mx[i][j]=max (mx[i][j-1],mx[i+ (1<< (J-1))][j-1]); A } at } - } - intQuery (intLintR) - { - intJ; - for(j=0;(1<<J) <= (r-l+1, j + +); j--; in returnMax (mx[l][j],mx[r-(1<<J) +1][J])-min (mn[l][j],mn[r-(1<<J) +1][j]); - } to intMain () + { - intT,k,i,left,right; the Long Longans; *t=read (); $ while(t--)Panax Notoginseng { -N=read (); k=read (); the for(i=1; i<=n;i++) a[i]=read (); + ST (); Aleft=1;//left End (the left end must be monotonically incremented) theans=0; + for(right=1; right<=n;right++)//Enumerate right Endpoints - { $ while(Query (left,right) >=k&&left<right) left++;//moves the left endpoint while enumerating the right endpoint. Each time you change the right endpoint, the left end point is only likely to be unchanged or moved to the right. $ans+= ((Long Long) right-left+1ll);//The substring of the statistic is the right end point and the leftmost one is between the right endpoint and the left endpoint. - } -printf"%lld\n", ans); the } - fclose (stdin);Wuyi fclose (stdout); the return 0; -}
Hdu 5289-assignment greedy, ST table