HDU 5289 Assignment (monotone queue)

Source: Internet
Author: User
Tags cmath

Test instructions: to the T-foot data, and then each group of N and K, indicating the number of N, k means the maximum allowable capacity is poor, the next n number represents the ability of n individuals, the ability to poor in K within the interval has several


Analysis: Maintain a range of maximum and minimum values, so that their difference is less than k, so the use of a monotone queue


Common monotone Queue procedure:

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std;    const int MAXN = 1e6+5;int a[maxn];struct node{int index; int v;}    Qd[maxn];node Qx[maxn];int Main () {int t;    scanf ("%d", &t);        while (t--) {int n,k;        scanf ("%d%d", &n,&k);        for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);        } int st1,st2,ed1,ed2;        St1=st2=ed1=ed2=1;        Long Long sum=0;        int j=1;                for (int i=1;i<=n&&j<=n;i++) {if (i==1) {qd[1].index=qx[1].index=1;            QD[1].V=QX[1].V=A[1];  } else{while (st1<=ed1) {///monotonic queue Maintenance Max if (Qd[ed1].v<=a[i])  ed1--;                Smaller than a[i] and subscript smaller than I out of queue else break;               } Qd[++ed1].v=a[i];                A[i] into the queue qd[ed1].index=i;                while (ST2&LT;=ED2) {Monotonic Queue Maintenance Minimum if (qx[ed2].v>=a[i]) ed2--;                Larger than A[i] and subscript is smaller than I out of the queue else break;               } Qx[++ed2].v=a[i];                A[i] into the queue qx[ed2].index=i; while (QD[ST1].V-QX[ST2].V&GT;=K&AMP;&AMP;ST1&LT;=ED1&AMP;&AMP;ST2&LT;=ED2)//Count {if (q                    D[ST1].INDEX==J) st1++;                    if (qx[st2].index==j) st2++;                    sum+= (I-J);                j + +;            }}} while (J<=n) {sum+= (n-j+1);        j + +;    } printf ("%i64d\n", sum); }}

Two-point monotone queue procedure:

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std;    const int MAXN = 1e6+5;int a[maxn];struct node{int index; int v;}        Qd[maxn];node qx[maxn];int maxc (int l,int r,int D) {//two min to find the D into the queue until the while (l<=r) {int mid= (L+R)/2;        if (Qd[mid].v==d) return mid;        else if (qd[mid].v>d) l=mid+1;    else r=mid-1; } return L;}        int minc (int l,int r,int D) {//two min to find the D into the queue so far while (l<=r) {int mid= (L+R)/2;        if (Qx[mid].v==d) return mid;        else if (qx[mid].v<d) l=mid+1;    else r=mid-1; } return L;}    int main () {int t;    scanf ("%d", &t);        while (t--) {int n,k;        scanf ("%d%d", &n,&k);        for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);        } int st1,st2,ed1,ed2;        St1=st2=ed1=ed2=1;        Long Long sum=0;        int j=1; for (int i=1;i<=n&&j<=n;i++) {if (i==1) {qd[1].Index=1;                QD[1].V=A[1];                Qx[1].index=1;            QX[1].V=A[1];                         } else{Ed1=maxc (St1,ed1,a[i]);//two points to find the D into the queue so far, maintain the maximum value qd[ed1].v=a[i];                Qd[ed1].index=i; Ed2=minc (St2,ed2,a[i]);                Two points find D into the queue so far, maintain the minimum value qx[ed2].v=a[i];                Qx[ed2].index=i; while (QD[ST1].V-QX[ST2].V&GT;=K&AMP;&AMP;ST1&LT;=ED1&AMP;&AMP;ST2&LT;=ED2)//Count {if (qd[s                    T1].INDEX==J) st1++;                    if (qx[st2].index==j) st2++;                    sum+= (I-J);                j + +;            }}} while (J<=n) {sum+= (n-j+1);        j + +;    } printf ("%i64d\n", sum); }}



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

HDU 5289 Assignment (monotone 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.