HDU 3530 subsequence (dp+ monotone queue)

Source: Internet
Author: User

Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=3530

Test instructions
Find a longest interval where the difference between the maximum and minimum values is greater than or equal to or less than K

Analysis:
Maintaining the maximum and minimum values, and then the difference between the maximum maximum value and the minimum minimum value is not greater than Y, which is greater than y who deleted in front of the record starting point.

The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <    queue>using namespace Std;const int maxn = 100010;int A[maxn];int main () {int n,m,k;        while (~SCANF ("%d%d%d", &n,&m,&k)) {for (int i=1;i<=n;i++) scanf ("%d", &a[i]);        Deque<int > Mmax;        Deque<int > Mmin;        int ans = 0;        int st = 0;            for (int i=1;i<=n;i++) {while (!mmax.empty () &&a[i]>a[mmax.back ())) mmax.pop_back ();//Maintenance monotonicity            while (!mmin.empty () &&a[i]<a[mmin.back ()]) mmin.pop_back ();//Maintenance monotonicity mmax.push_back (i);            Mmin.push_back (i); while (!mmax.empty () &&!mmin.empty () &&a[mmax.front ()]-a[mmin.front ()]>k) {if (Mmax.front () &                    Lt;mmin.front ()) {st = Mmax.front ();                Mmax.pop_front ();             } else {St=mmin.front ();       Mmin.pop_front ();                }} if (!mmax.empty () &&!mmin.empty () &&a[mmax.front ()]-a[mmin.front ()]>=m)        ans = max (ans,i-st);    } cout<<ans<<endl; } return 0;}


HDU 3530 subsequence (dp+ 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.