BZOJ2276: [Poi2011]Temperature

n<=1000000个数,每个数的选择范围在Li到Ri<=1000000000之间,求最长能得到多长的连续不下降序列。

首先可以暴力,f[i][j]表示前i个数,最后一个数取j,然后瞎转移就好了,显然过不了。

仔细一想,转移过来的状态只有几个,用{x,y}表示一个路径,最后一个数为x,答案为y。x越大,y应该越大。而每次扫到一个新区间,要新开点的话,肯定选最小的点新开。于是就变成这样:

右边那个圈状态更新答案然后删掉,左边那个圈记y的最大值Max然后丢给{ai,Max},加进去。

区间操作用线段树???傻了吧,只有在前后操作,中间加一用全局变量瞎搞下就好啦,所以就写个单调队列嘛!

至于中间那段怎么加一可以去看NOIP2016蚯蚓。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<algorithm>
 5 //#include<iostream>
 6 using namespace std;
 7
 8 int n;
 9 #define maxn 1000011
10 int que[maxn],da[maxn],head,tail,a,b;
11 int main()
12 {
13     scanf("%d",&n);int ans=0;
14     head=tail=0;
15     for (int i=1;i<=n;i++)
16     {
17         scanf("%d%d",&a,&b);
18         int tmp=0;
19         while (head<tail && que[head]>b) ans=max(ans,da[head++]+i-1);
20         while (head<tail && que[tail-1]<=a) tmp=max(tmp,da[--tail]+i-1);
21         int now=tmp-i+1;
22         while (head<tail && da[tail-1]<=now) tail--;
23         if (head<tail && a>que[tail-1]) continue;
24         que[tail]=a,da[tail++]=now;
25     }
26     for (int i=head;i<tail;i++) ans=max(ans,da[i]+n);
27     printf("%d\n",ans);
28     return 0;
29 }

时间: 2024-10-13 20:44:02

BZOJ2276: [Poi2011]Temperature的相关文章

bzoj2276: [Poi2011]Temperature(单调队列/堆)

这题有两种写法,而且是完全(几乎?)不一样的写法...并不是换了个方法来维护而已 单调队列O(N):用一个队列维护a[]的单调递减,对于每个i满足a[队头]<=b[i],然后就可以算出以每一位为结尾的最大答案了 #include<stdio.h> #include<cstring> #include<iostream> #include<cstdlib> using namespace std; const int maxn=1000010,inf=1

[BZOJ] 2276: [Poi2011]Temperature

2276: [Poi2011]Temperature Time Limit: 20 Sec  Memory Limit: 32 MBSubmit: 731  Solved: 334[Submit][Status][Discuss] Description The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The measurement is done automatically, an

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

Description 某国进行了连续n天的温度测量,测量存在误差,测量结果是第i天温度在[l_i,r_i]范围内. 求最长的连续的一段,满足该段内可能温度不降 第一行n 下面n行,每行l_i,r_i 1<=n<=1000000 一行,表示该段的长度 Sample Input 6 6 10 1 5 4 8 2 5 6 8 3 5 Sample Output 4 ------------------------------------ 这道题其实就是维护一个连续的不下降序列 考虑维护一个队列 对

BZOJ 2276 Poi2011 Temperature 单调队列

题目大意:给定一个序列,每个元素的大小有一个取值范围,求一段区间满足区间内元素可能单调不降 对L维护一个单调不增的单调队列,一旦新插入的R值比队头的L值小就把队头弹掉 这样可以保证单调队列中的元素是合法的极大子区间 然后更新答案就行了 乱写读入优化害死人啊QwQ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1001001 usi

STM32 F4 ADC DMA Temperature Sensor

STM32 F4 ADC DMA Temperature Sensor Goal: detecting temperature variations using a temperature sensor, ADC with DMA and TIM3 as a trigger (ADC sampling frequency = TIM3 trigger frequency). Note: Using TIM3 as a trigger is suited for monitoring temper

Rising Temperature

Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. +---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+------------+----

BZOJ2212: [Poi2011]Tree Rotations

2212: [Poi2011]Tree Rotations Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 391  Solved: 127[Submit][Status] Description Byteasar the gardener is growing a rare tree called Rotatus Informatikus. It has some interesting features: The tree consists o

Temperature hdu 3477

Temperature Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 650    Accepted Submission(s): 208 Problem Description Many people like summer as summer has a lot of advantages, but on the other han

[LeetCode]-DataBase-Rising Temperature

Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. +---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+------------+----