当前位置:网站首页>洛谷P3522 [POI2011]TEM-Temperature 题解
洛谷P3522 [POI2011]TEM-Temperature 题解
2022-07-17 18:55:00 【q779】
洛谷P3522 [POI2011]TEM-Temperature 题解
题目链接:P3522 [POI2011]TEM-Temperature
题意:
某国进行了连续 n n n ( 1 ≤ n ≤ 1 , 000 , 000 1 \le n \le 1,000,000 1≤n≤1,000,000)天的温度测量,测量存在误差,测量结果是第 i i i 天温度在 [ l i , r i ] [l_i,r_i] [li,ri] 范围内。
求最长的连续的一段,满足该段内可能温度不降。
不难发现合法区间为 max { l i } > r i + 1 \max\{l_i\}>r_{i+1} max{ li}>ri+1
考虑单调队列维护最大值
注意答案的更新应该用(i-1)-q[st]+1
因为如果有个 l i l_i li 特别大,把当前维护的合法的 l l l 都给pop掉了
计数不应当是从现在的 l i l_i li 开始,而是最后一个被pop掉的
这样用q[st]正好(我的代码是(l,r]的)
时间复杂度 O ( n ) O(n) O(n)
代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <random>
using namespace std;
// #define int long long
// #define INF 0x3f3f3f3f3f3f3f3f
namespace FastIO
{
#define gc() readchar()
#define pc(a) putchar(a)
#define SIZ (int)(1e6+15)
char buf1[SIZ],*p1,*p2;
char readchar()
{
if(p1==p2)p1=buf1,p2=buf1+fread(buf1,1,SIZ,stdin);
return p1==p2?EOF:*p1++;
}
template<typename T>void read(T &k)
{
char ch=gc();T x=0,f=1;
while(!isdigit(ch)){
if(ch=='-')f=-1;ch=gc();}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
k=x*f;
}
template<typename T>void write(T k)
{
if(k<0){
k=-k;pc('-');}
static T stk[66];T top=0;
do{
stk[top++]=k%10,k/=10;}while(k);
while(top){
pc(stk[--top]+'0');}
}
}using namespace FastIO;
#define N (int)(1e6+15)
int n,st,en,res=1,l[N],r[N],q[N];
signed main()
{
read(n); st=en=1;
for(int i=1; i<=n; i++)
{
read(l[i]);read(r[i]);
while(st<en&&l[q[st+1]]>r[i])++st;
if(st<en)res=max(res,i-q[st]);
while(st<en&&l[q[en]]<l[i])--en;
q[++en]=i;
}
write(res);
return 0;
}
转载请说明出处
边栏推荐
猜你喜欢

如何优雅的升级 Flink Job?

ModuleNotFoundError: No module named ‘_distutils_hack‘

基于PMOS的过压保护(OVP)电路仿真
![[code hoof set novice village question 600] format specifier of float and double](/img/19/433f794617f3c3c72732f1a4efe252.png)
[code hoof set novice village question 600] format specifier of float and double

Weekly summary (*65): planned output

Onvif protocol related: 2.1.2 get screenshot URL in none mode

codeforce:G. Good Key, Bad Key【贪心】

codeforce:A. Doremy‘s IQ【反向贪心】

STM32F1与STM32CubeIDE编程实例-MPU-6050 六轴(陀螺仪 + 加速度计)驱动

Principle of voice communication network
随机推荐
【Acwing】第60场周赛 题解
The cloud audit service CTS is a paid service. The paid items include the opening of tracker, event tracking, storage and retrieval of events within 7 days and other related fees
鸿蒙设备开发快速入门之Helloword与LED——华为云14天鸿蒙设备开发实战学习笔记 第二篇
[postgraduate entrance examination vocabulary training camp] day 7 - second, attract, current, collect, simple, communicate, vocation
力扣第 302 场周赛
[7.14] code source - [open box] [XOR inverse] [continuous subsequence] [trigonometric fruit count]
统计直播间的榜一信息,从这里起步
【动态规划】—— 最长上升子序列模型
[micro Service ~ advanced] configuration center practice
【7.14】代码源 -【拆方块】【XOR Inverse】【连续子序列】【三角果计数】
Transphorm的表面贴装封装产品系列增加行业标准TO-263 (D2PAK)封装产品,扩大SuperGaN平台的优势
565. Array nesting
565.数组嵌套
"Podcast with relish" 392 original soup, original food: midsummer night, mashed horse, kebab, and pull board
健康防猝指南3:健康保健
[从零开始学习FPGA编程-53]:高阶篇 - 基于IP核的FPGA开发-PLL锁相环IP核的原理与配置(Xilinx)
STM32F1与STM32CubeIDE编程实例-MPU-6050 六轴(陀螺仪 + 加速度计)驱动
No.3汇编进阶
[understanding of opportunity-46]: Guiguzi - Chapter 10 - schemer, meaning of wisdom
【考研词汇训练营】Day 6 —— eventually,state,create,productivity,stimulate