[Multi-school 2015.01.1002 monotone queue] Hdu 5289 Assignment

Source: Internet
Author: User

Test instructions

Give you n people and a K

Ask you to divide this n person into how many contiguous sub-intervals, require interval each number 22 difference absolute value is less than K

Ideas:

We just need the position where the leftmost and absolute difference is greater than or equal to K.

Suppose for I this position, the leftmost position is TEP, does not exist the word tep=0

Then when and where the contribution is sum[i]=min (i-tep,sum[i-1]+1);

So for this position, I used two monotone queues.

For the first time, the increment is maintained and the second maintenance is decremented.

Each time the unsatisfied point all out of the team, and then record the largest subscript

And then the team

Code:

#include "stdio.h" #include "algorithm" #include "string.h" #include "iostream" #include "queue" #include "map" #include " Vector "#include" string "#include" Cmath "using namespace std; #define N 222222#define ll __int64struct node{int x,s;};    Node Q[n],v[n];ll sum[n];int finde_max (int l,int r,int k) {int ans=-1;        while (l<=r) {int mid= (L+R)/2;            if (q[mid].s>k) {ans=mid;        l=mid+1;    } else r=mid-1; } return ans;    int finde_min (int l,int r,int k) {int ans=-1;        while (l<=r) {int mid= (L+R)/2;            if (q[mid].s<k) {ans=mid;        l=mid+1;    } else r=mid-1; } return ans;    int main () {int t;    cin>>t;        while (t--) {int n,k;        scanf ("%d%d", &n,&k);            for (int i=1; i<=n; i++) {scanf ("%d", &AMP;V[I].S);        V[i].x=i;        } int head=0,ed=-1;        memset (sum,0,sizeof (sum));        ll Ans=0; For (inT I=1; i<=n;            i++) {int tep=0;                    if (head<=ed) {while (ABS (Q[HEAD].S-V[I].S) >=k && head<=ed) {                    Tep=max (tep,q[head].x);                head++;            }} Sum[i]=tep;            if (head>ed) q[++ed]=v[i];                else {int kx=finde_min (HEAD,ED,V[I].S);                if (kx==-1) q[ed=head]=v[i];            else Q[ed=kx+1]=v[i];        }} Head=0,ed=-1;            for (int i=1; i<=n; i++) {int tep=0;                    if (head<=ed) {while (ABS (Q[HEAD].S-V[I].S) >=k && head<=ed) {                    Tep=max (tep,q[head].x);                head++;            }} Sum[i]=max (Sum[i], (LL) tep);            if (head>ed) q[++ed]=v[i];   else {int Kx=finde_max (HEAD,ED,V[I].S);             if (kx==-1) q[ed=head]=v[i];            else Q[ed=kx+1]=v[i];        }} ll haha=0;            for (int i=1; i<=n; i++) {haha=min (haha+1,i-sum[i]);        Ans+=haha;    } printf ("%i64d\n", ans); } return 0;}


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

[Multi-school 2015.01.1002 monotone queue] Hdu 5289 Assignment

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.