HDU 5289 Assignment (RMQ monotone (double-ended) queue)

Source: Internet
Author: User
Tags cmath

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5289


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


Test instructions
An integer sequence is given to find out how many intervals satisfy the difference between the maximum element and the minimum element in the interval not exceeding K ".

Ps:

1: You can first use RMQ to process the maximum value of the interval, and then enumerate the interval! Certainly blindly enumeration certainly does not have the following two kinds of methods fast!

2: Maintain interval max with monotonic (double-ended) queue

3: Enumerate the left endpoint, the right end of the dichotomy, and the St algorithm to find the interval maximum value

Code one is as follows:

#include <cstdio> #include <cstring> #include <cmath> #include <iostream>using namespace std; const int MAXN = 100117;int num[maxn];int f_min[maxn][30],f_max[maxn][30];void Init (int n) {for (int i = 1; I <= n; i    + +) {f_min[i][0] = f_max[i][0] = Num[i]; } for (int i = 1; (1<<i) <= N; i++)//increment order by interval length recursive {for (int j = 1; j+ (1<<i)-1 <= N; j + +)//Interval start {f_max[j][i] = ma            X (f_max[j][i-1],f_max[j+ (1<< (i-1))][i-1]);        F_min[j][i] = Min (f_min[j][i-1],f_min[j+ (1<< (i-1))][i-1]);    }}}int Query_max (int l,int r) {int k = (int) (log (double (r-l+1))/log ((double) 2)); Return Max (F_max[l][k], f_max[r-(1<<k) +1][k]);}    int query_min (int l,int r) {int k = (int) (log (double (r-l+1))/log ((double) 2)); return min (F_min[l][k], f_min[r-(1<<k) +1][k]);} int solve (int l, int r) {return Query_max (l,r)-query_min (l,r);}    int main () {int t;    int n, K;    scanf ("%d", &t); While(t--)        {scanf ("%d%d", &n,&k);        for (int i = 1; I <= n; i++) {scanf ("%d", &num[i]);        } Init (n);        __int64 ans = 0;        int pos = 1;                for (int i = 1; I <= n; i++) {while (Solve (POS, i) >= K && Pos < i) {            pos++;        } ans+=i-pos+1;    } printf ("%i64d\n", ans); } return 0;}

Code two is as follows: http://www.bubuko.com/infodetail-987302.html
#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std;    Define LL __int64deque <LL> deq1, deq2;//monotone queue, deq1 max, deq2 min LL a[100010]; int main () {int T, n, I, J;    LL k, ans;    scanf ("%d", &t);        while (t--) {scanf ("%d%i64d", &n, &k);        for (i = 0; i < n; i++) scanf ("%i64d", &a[i]);            if (k = = 0) {printf ("0\n");        Continue;        } while (!deq1.empty ()) Deq1.pop_back ();        while (!deq2.empty ()) Deq2.pop_back (); for (i = 0, j = 0, ans = 0; i < n; i++)//i in front, J in after {while (!deq1.empty () && deq1.back () &l T            A[i]) deq1.pop_back ();            Deq1.push_back (A[i]);            while (!deq2.empty () && deq2.back () > A[i]) deq2.pop_back ();            Deq2.push_back (A[i]); while (!deq1.empty () &&!deq2.empty () && Deq1.front ()-Deq2.front () >= k) {ans + = (i-j);                printf ("%d%d,%i64d%i64d\n", I, J, Deq1.front (), Deq2.front ());                if (deq1.front () = = A[j]) Deq1.pop_front ();                if (deq2.front () = = A[j]) Deq2.pop_front ();            j + +;            }} while (J < N) {ans + = (i-j);        j + +;    } printf ("%i64d\n", ans); } return 0;}

The code three is as follows: http://www.bubuko.com/infodetail-987919.html

#include <cstdio> #include <cstring> #include <cmath> #define LL long Long#define Max (a) (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;const int n=200007;int minn[n][20];//2^18=262144 2^20=1048576int maxx[n][20];//----------- -----------Query O (1)-------------int querymin (int l,int r) {int K=floor (log2 (double) (r-l+1)),//2^k <= (r-l + 1), Flo The OR () takes the whole function down to return Min (minn[l][k],minn[r-(1<<k) +1][k]);}    int Querymax (int l,int r) {int K=floor (log2 (double) (r-l+1)); Return Max (maxx[l][k],maxx[r-(1<<k) +1][k]);}    -------------------------------------------------int calc (int l,int r) {int k=log2 ((double) (r-l+1));    int Max=max (maxx[l][k],maxx[r-(1<<k) +1][k]);    int Min=min (minn[l][k],minn[r-(1<<k) +1][k]); return max-min;}    int main () {int T;    int n,k,i,j,p;    LL ans;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&k); for (I=1; i<=n; ++i) {scanf ("%d",&AMP;J);        Minn[i][0]=maxx[i][0]=j; }//------------------------------------------pretreatment o (nlogn)---------------for (j=1; (1&LT;&LT;J) <=n; ++J)//1<<j==2^j, enumeration interval length 1,2,4,8,16,,,,, for (I=1; i+ (1<<j) -1<=n; ++i)//i+ (1<<j)-1 denotes interval right boundary, enumeration interval                Left Border {p= (1<< (j-1));                Minn[i][j]=min (Minn[i][j-1],minn[i+p][j-1]);            Maxx[i][j]=max (Maxx[i][j-1],maxx[i+p][j-1]); }//-----------------------------------------------------------------------//---------------------------Enumerate the left endpoint,        The two-point right endpoint---------------------------int l,r,mid;            The ans=0;//left endpoint is fixed to I, the right endpoint is determined with L,r,mid, and finally with one of L and R, at this time l+1==r for (i=1; i<=n; ++i) {l=i,r=n; while (L+1<r) {mid= (l+r) >>1;//(l+r)/2== (l+r) >>1 if (Calc (i,mid) &L                t;k) {l=mid; } else {r=Mid-1;//himself to demonstrate the algorithm flow know that R can be assigned Mid-1}} if (Calc (i,r) <k) {ans            =ans+ (LL) (r-i+1);            } else {ans=ans+ (LL) (l-i+1);    }}//---------------------------------------------------------------------------printf ("%lld\n", ans); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5289 Assignment (RMQ monotone (double-ended) queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.