The 6e-difference value cannot exceed the number of sub-segments in K.

Source: Internet
Author: User

Give N number, and then find the longest subsequence (no need to be continuous), so that the difference between the maximum and minimum values of this subsequence cannot exceed K. find several sub-sequences that meet the requirements and output their starting and ending positions.

#include <cstdio>#include <iostream>using namespace std;int n,k,a[100005];int Max[100005],Min[100005],h1,t1,h2,t2,j;inline int Get_Max(){    return a[Max[h1+1]];}inline int Get_Min(){    return a[Min[h2+1]];}inline void Update(){    while (Max[h1+1]<j) h1++;    while (Min[h2+1]<j) h2++;}inline void Push(int x){    while (h1!=t1 && a[x]>=a[Max[t1]]) t1--;    Max[++t1]=x;    while (h2!=t2 && a[x]<=a[Min[t2]]) t2--;    Min[++t2]=x;}int ans=1;int _O_[100005],_cnt=0;int main(){    scanf("%d%d",&n,&k);    for (int i=1;i<=n;i++) scanf("%d",a+i);    Push(1);    _O_[_cnt=1]=1;    Max[t1=1]=Min[t2=1]=1;    j=1;    for (int i=2;i<=n;i++)    {        Push(i);        while (Get_Max()-Get_Min()>k) j++,Update();        if (i-j+1>ans)        {            ans=i-j+1;            _O_[_cnt=1]=j;        }        else if (i-j+1==ans)        {            _O_[++_cnt]=j;        }    }    printf("%d %d\n",ans,_cnt);    for (int i=1;i<=_cnt;i++)        printf("%d %d\n",_O_[i],_O_[i]+ans-1);    return 0;}

 

The 6e-difference value cannot exceed the number of sub-segments in K.

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.