BZOJ 2276: [Poi2011]Temperature 单调队列
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 单调队列的更多相关文章
- bzoj 2276: [Poi2011]Temperature——单调队列
Description 某国进行了连续n天的温度测量,测量存在误差,测量结果是第i天温度在[l_i,r_i]范围内. 求最长的连续的一段,满足该段内可能温度不降 第一行n 下面n行,每行l_i,r_i ...
- bzoj 2276 [ Poi 2011 ] Temperature —— 单调队列
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2276 维护 l 递减的单调队列,队头的 l > 当前的 r 就出队,因为不能是连续一段 ...
- BZOJ 1047 二维单调队列
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...
- BZOJ 1855 股票交易(单调队列优化DP)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1855 题意:最近lxhgww又迷上了投资股票, 通过一段时间的观察和学习,他总结出了股票 ...
- BZOJ 2424 订货(贪心+单调队列)
怎么题解都是用费用流做的啊...用单调队列多优美啊. 题意:某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初 ...
- BZOJ 1012 线段树||单调队列
非常裸的线段树 || 单调队列: 假设一个节点在队列中既没有时间优势(早点入队)也没有值优势(值更大),那么显然不管在如何的情况下都不会被选为最大值. 既然它仅仅在末尾选.那么自然能够满足以上的条件 ...
- bzoj 2216: Lightning Conductor 单调队列优化dp
题目大意 已知一个长度为\(n\)的序列\(a_1,a_2,...,a_n\)对于每个\(1\leq i\leq n\),找到最小的非负整数\(p\)满足: 对于任意的\(j\), \(a_j \le ...
- BZOJ 2096: [Poi2010]Pilots 单调队列
Code: #include<bits/stdc++.h> #define maxn 4000000 using namespace std; void setIO(string s) { ...
- BZOJ : [Usaco2013 Nov]Crowded 单调队列
正反两遍个来一次单调队列 DP 即可. Code: #include<cstdio> #include<deque> #include<algorithm> usi ...
随机推荐
- 创建SharePoint 2010 Timer Job
好久没有写博客了. 近期在使用SharePoint 2010中Timer Job的功能,有了一点心得,分享一下. 我个人觉得SharePoint Timer Job和Windows Service或者 ...
- VFL语言简洁
一.VFL语言简洁 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...
- 《iOS Human Interface Guidelines》——Wallet
Wallet Wallet(钱包)帮助人们查看和管理Pass(凭证),这是一种相似于登机牌.优惠券.会员卡.奖励卡和各种票的物理凭证的数字替代.Wallet也同意人们加入信用卡.借记卡和储值卡来和Ap ...
- POJ 2367:Genealogical tree(拓扑排序)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Spe ...
- 王立平--SQLite,SQLiteOpenHelper的简单应用
Android平台提供给我们一个数据库辅助类来创建或打开数据库,这个辅助类继承自SQLiteOpenHelper类.在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象.继承 ...
- Knowing When to Use Override and New Keywords (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173153.aspx In C#, a method in a derived class can have t ...
- P1402 酒店之王 网络流
大水题,我自己瞎做就做出来了,没啥说的,zz建图,就是板子. 题干: 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的房间色调.阳光等, ...
- MSP430:输入捕获
在做超声模块时用到 //捕获上升沿 void Capture_Pos(void) { P2SEL |= Echo; //选择P23作为捕捉的输入端子 Timer1_A //TA1CCTL1 |=CM_ ...
- tinymce 出现 Uncaught (in promise) TypeError: ae(...).createObjectURL is not a function
需要引入两个JS文件:jQuery.tinymce.min.js 和 tinymce.min.js <script type="text/javascript" src=&q ...
- hebernate基础学习2---属性
---------------------------------------------------------------------------------------------------- ...