Hud-3530-Subsequence-maintain two monotonous queues

Source: Internet
Author: User

Maintain two monotonous queues, one storing the maximum value before the current vertex.

In addition, the minimum value before the current vertex is stored.

If the difference between the maximum value and the minimum value is greater than k, move backward from the front of the maximum and minimum values.

#include
 
  #include
  
   #include#include
   
    #include
    
     using namespace std;//#define INF ((1<<30)-1)#define INF 0xfffff#define maxn 220000#define LL long long#define MOD 1000000009struct list{    int val;    int x;}p[maxn],q[maxn],pp;int main(){    int n,m,k,i,x;    while(~scanf("%d%d%d",&n,&m,&k))    {        int h1,h2,t1,t2;        h1=h2=1;        t1=t2=0;        int last1,last2,ans;        ans=last1=last2=0;        for(i=1;i<=n;i++)        {            scanf("%d",&x);            pp.x=i;            pp.val=x;            while(t1>=h1&&p[t1].val
     
      =h2&&q[t2].val>x)t2--;            q[++t2]=pp;            while(p[h1].val-q[h2].val>k)            {                if(p[h1].x
      
       =m) { ans=max(ans,i-max(last1,last2)); } } cout<
       

Related Article

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.