Description

The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The measurement is done automatically, and its result immediately printed. Unfortunately, the ink in the printer has long dried out... The employees of BIM however realised the fact only recently, when the Byteotian Organisation for Meteorology (BOM) requested access to that data.

An eager intern by the name of Byteasar saved the day, as he systematically noted down the temperatures reported by two domestic alcohol thermometers placed on the north and south outside wall of the BIM building. It was established decades ago by various BIM employees that the temperature reported by the thermometer on the south wall of the building is never lower than the actual temperature, while that reported by the thermometer on the north wall of the building is never higher than the actual temperature. Thus even though the exact temperatures for each day remain somewhat of a mystery, the range they were in is known at least.

Fortunately for everyone involved (except Byteasar and you, perhaps), BOM does not require exact temperatures. They only want to know the longest period in which the temperature was not dropping (i.e. on each successive day it was no smaller than on the day before). In fact, the veteran head of BIM knows very well that BOM would like this period as long as possible. To whitewash the negligence he insists that Byteasar determines, based on his valuable notes, the longest period in which the temperature could have been not dropping. Now this is a task that Byteasar did not quite expect on his BIM internship, and he honestly has no idea how to tackle it. He asks you for help in writing a program that determines the longest such period.

某国进行了连续n天的温度测量,测量存在误差,测量结果是第i天温度在[l_i,r_i]范围内。
求最长的连续的一段,满足该段内可能温度不降。

Input

In the first line of the standard input there is one integer n(1<=N<=1000000) that denotes the number of days for which Byteasar took notes on the temperature. The measurements from day are given in the line no.i+1 Each of those lines holds two integers, x and y (-10^9<=x<=y<=10^9). These denote, respectively, the minimum and maximum possible temperature on that particular day, as reported by the two thermometers.

In some of the tests, worth 50 points in total, the temperatures never drop below -50 degrees (Celsius, in case you wonder!) and never exceeds 50 degrees (-50<=x<=y<=50)

第一行n
下面n行,每行l_i,r_i
1<=n<=1000000

Output

In the first and only line of the standard output your program should print a single integer, namely the maximum number of days for which the temperature in Byteotia could have been not dropping.

一行,表示该段的长度

题解:

维护一个关于 $l$ 的单调递减队列,保证队头的 $l$ 小于等于队尾的 $r$.

发现新加入的 $l$ 若大于等于先前的一个 $r$,则之前的那个 $r$ 就是无用的.

讲起来挺费劲,画一画大概能感性理解吧

#include<bits/stdc++.h>
#define maxn 3000000
using namespace std;
void setIO(string s)
{
string in=s+".in";
freopen(in.c_str(),"r",stdin);
}
deque<int>q;
int l[maxn],r[maxn];
int main()
{
// setIO("input");
int n,i,j,x=0,y,ans=0;
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%d%d",&l[i],&r[i]);
for(i=1;i<=n;++i)
{
while(!q.empty() && l[q.back()] < l[i]) q.pop_back();
q.push_back(i);
while(!q.empty() && l[q.front()] > r[i]) x=q.front(), q.pop_front();
ans=max(ans, i-x);
}
printf("%d\n",ans);
return 0;
}

  

BZOJ 2276: [Poi2011]Temperature 单调队列的更多相关文章

  1. bzoj 2276: [Poi2011]Temperature——单调队列

    Description 某国进行了连续n天的温度测量,测量存在误差,测量结果是第i天温度在[l_i,r_i]范围内. 求最长的连续的一段,满足该段内可能温度不降 第一行n 下面n行,每行l_i,r_i ...

  2. bzoj 2276 [ Poi 2011 ] Temperature —— 单调队列

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2276 维护 l 递减的单调队列,队头的 l > 当前的 r 就出队,因为不能是连续一段 ...

  3. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  4. BZOJ 1855 股票交易(单调队列优化DP)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1855 题意:最近lxhgww又迷上了投资股票, 通过一段时间的观察和学习,他总结出了股票 ...

  5. BZOJ 2424 订货(贪心+单调队列)

    怎么题解都是用费用流做的啊...用单调队列多优美啊. 题意:某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初 ...

  6. BZOJ 1012 线段树||单调队列

    非常裸的线段树  || 单调队列: 假设一个节点在队列中既没有时间优势(早点入队)也没有值优势(值更大),那么显然不管在如何的情况下都不会被选为最大值. 既然它仅仅在末尾选.那么自然能够满足以上的条件 ...

  7. bzoj 2216: Lightning Conductor 单调队列优化dp

    题目大意 已知一个长度为\(n\)的序列\(a_1,a_2,...,a_n\)对于每个\(1\leq i\leq n\),找到最小的非负整数\(p\)满足: 对于任意的\(j\), \(a_j \le ...

  8. BZOJ 2096: [Poi2010]Pilots 单调队列

    Code: #include<bits/stdc++.h> #define maxn 4000000 using namespace std; void setIO(string s) { ...

  9. BZOJ : [Usaco2013 Nov]Crowded 单调队列

    正反两遍个来一次单调队列 DP 即可. Code: #include<cstdio> #include<deque> #include<algorithm> usi ...

随机推荐

  1. iOS中xib与storyboard原理,与Android界面布局的异同

    用文本标记语言来进行布局,用的最多的应该是HTML语言.HTML能够理解为有一组特殊标记的XML语言. 一.iOS中xib与storyboard显示原理 在iOS中基本的布置界面的方式有3种:代码.x ...

  2. react面试题(一)

    react 生命周期函数 初始化阶段: getDefaultProps:获取实例的默认属性 getInitialState:获取每个实例的初始化状态 componentWillMount:组件即将被装 ...

  3. OCX 打包 CAB 与 JS 调用具体教程

        近期在做一个 WEB 项目.须要调用 OCX 进行连接读卡器读卡.本来并不想用 OCX 技术.由于 ActiveX 技术是微软出品.这样就导致整个系统仅仅能使用 IE 浏览器(其它浏览器能够通 ...

  4. struts2 与spring整合

    要把struts2的action交给spring管理,这样spring才干帮struts2注入须要的的bean(一開始action是由struts初始化,所以想注入spring里面的bean是注入不了 ...

  5. swift手记-6

    // // ViewController.swift // learn // // Created by myhaspl on 16/1/26. // Copyright (c) 2016年 myha ...

  6. Linux对外连接port数限制

    左右时,開始大量抛例如以下异常: java.net.BindException:Cannot assign requested address atsun.nio.ch.Net.connect0(Na ...

  7. HDU 1299Diophantus of Alexandria

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. 【POJ 3614】 Sunscreen

    [题目链接] http://poj.org/problem?id=3614 [算法] 将MinSPF从大到小排序,每头牛找SPF值最大的防晒霜 [代码] #include <algorithm& ...

  9. JDK8 函数式接口

    JDK8中为了适应函数式响应编程模式,引入了函数式接口概念以增加Lambda表达式的功能.函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:SAM类型的接口(Single Abstract M ...

  10. Spark 机器学习 ---CountVectorizer

    文本特征提取->> CountVectorizer:基于词频数的文档向量 package Spark_MLlib import org.apache.spark.ml.feature.Co ...