Monotonic Queue: Temperature

Source: Internet
Author: User

The main idea: a country carried out a continuous n-day temperature measurement, measuring the existence of errors, the measurement result is the first day temperature within the [L_i,r_i] range.
The longest continuous paragraph satisfies the possibility that the temperature does not drop.
First row n
The following n lines, each line L_i,r_i
1<=n<=1000000
A row that represents the length of the segment
Sample Input
6
6 10
1 5
4 8
2 5
6 8
3 5
Sample Output
4

Knowledge points: recursive, monotone queue

1#include <bits/stdc++.h>2 using namespacestd;3queue<int>Q;4 intl[1000005],r[1000005];5 intq[1000005];6 intn,t;7 intMax,ans,head,tail;8 intMain () {9     //freopen ("temperature.in", "R", stdin);Ten     //freopen ("Temperature.out", "w", stdout); OneCin>>N; A      for(intI=1; i<=n;i++) -scanf"%d%d",&l[i],&r[i]); -     /* the monotonic Queue Analysis: - 1. What is the time of the break: when you want to join the first day, its r[i]<l[j] (L in the last few days), this time, - to update the answer first, and then re-count the day after the first day of my death - 2. Operating with a non-descending monotone queue, there is a key: Assume that the L value for a certain period of time is + 36 35 34 32 29 The following day L value is 45 This queue is updated to 55 53 48 45 ... Because the temperature - the number of days back and L value is high, if this day can be, then the first six days must be, so the queue element is a. + but this does not work, can not determine the number of days information, so let q[] save the day A L[q[head]] to call, update Q is to update the L array, anyway is at O (n) to sweep back from the past without affecting the answer -     */ -Head=1; tail=0; -      for(intI=1; i<=n;i++){ -          while(L[q[head]]>r[i]&&head<=tail)//Q is the monotone minus queue, if the current L[q[head]]>r[i] -head++//The day cannot be connected from q[head], so continue backwards. in                                          //find, until L[q[head]]<r[i], because it is a monotone queue, so -                                          //The next few days will be sure to          +     if(Head<=tail)//Update ans -Ans=max (ans,i-q[head]+1); the              *         intt=i; $      while(l[i]>l[q[tail]]&&head<=tail)Panax Notoginsengt=q[tail],tail--; -l[t]=L[i]; theq[++tail]=T; +     } Acout<<ans; the     return 0; + } -  

Monotonic Queue: Temperature

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.