AssignmentTime
limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 627 Accepted Submission (s): 318
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 Input
24 23 1 2 410 50 3 4 5 2 1 6 7 8 9
Sample Output
528Hint
Authorfzuacm
Source2015 multi-university Training Contest 1
Problem Solving Ideas:
Enumerate left endpoints, find right end points, maintain interval maximums and minimums with RMQ
#include <iostream> #include <cstring> #include <cmath> #include <cstdlib> #include <cstdio > #include <queue> #include <vector> #include <stack> #define LL long longusing namespace Std;const int MAXN = 100000 + 10;int a[maxn];int dp1[maxn][20], dp[maxn][20];int N, M;void rmq_init_min () {for (int i=0;i<n;i++ ) dp1[i][0] = A[i]; for (int j=1, (1<<j) <= n;j++) {for (int i=0;i+ (1<<J)-1 < n;i++) {Dp1[i][j] = Min (dp1[i][j-1], Dp1[i + (1<< (j-1))][j-1]); }}}int rmq_min (int L, int R) {int k = 0; while (1<< (k+1) <= r-l + 1) k++; return min (Dp1[l][k], dp1[r-(1<<k) +1][k]);} void Rmq_init_max () {for (int i=0;i<n;i++) dp[i][0] = A[i]; for (int j=1, (1<<j) <= n;j++) {for (int i=0;i+ (1<<J)-1 < n;i++) {Dp[i][j] = Max (Dp[i][j-1], Dp[i + (1<< (j-1))][j-1]); }}}int Rmq_max (int L, int R) {int k = 0; while (1<< (k+1) <= r-l + 1) k++; Return Max (Dp[l][k], dp[r-(1<<k) +1][k]);} int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n, &m); for (int i=0;i<n;i++) scanf ("%d", &a[i]); Rmq_init_min (); Rmq_init_max (); A long long ans = 0; for (int i=0;i<n;i++) {int L = i, r = N-1; while (L <= r) {int mid = (L + r) >> 1; int low = Rmq_min (I, mid); int high = Rmq_max (i, mid); cout << L << ' << r << ' << high << ' <<low << Endl; if (High-Low < M) L = mid + 1; else R = mid-1; }//cout << l << Endl; Ans + = (l-i); } printf ("%i64d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5289 Assignment (more than 2015 school first game two minutes + RMQ)