题目链接:http://poj.org/problem?id=2823

用RMQ超时了,我想应该是不会的,看discuss说,之前RMQ过了。

维护两个单调队列。

单调递减的队列,每插入一个时:

超过单调队列长度,左移头指针。

第一个或者符合条件,直接加到后面。

否则,一直退;

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std; const int N = + ; int n,m;
int a[N],q[N]; void MinQ()
{
int h=,t=;
q[] = ;
for(int i=;i<=n;i++) {
if(i-q[h]==m) h++; if(t==h-||a[i]>a[q[t]]) {
t++;
q[t] = i;
}
else {
while(t>=h&&a[i]<=a[q[t]])
{
q[t] = i;
t--;
}
t++;
}
if(i>=m) printf("%d ",a[q[h]]); }
puts("");
} void MaxQ()
{
int h=,t=;
q[] = ;
for(int i=;i<=n;i++) { if(i-q[h]==m) h++; if(t==h-||a[i]<a[q[t]]) {
t++;
q[t] = i;
}
else {
while(t>=h&&a[i]>=a[q[t]])
{
q[t] = i;
t--;
}
t++; }
if(i>=m) printf("%d ",a[q[h]]); }
} int main()
{
cin>>n>>m;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
} MinQ();
MaxQ(); return ;
}

题目链接:http://acm.fzu.edu.cn/problem.php?pid=1894

和单调递增队列一样。

 /*
RunID: 727738
UserID: TreeDream
Submit time: 2017-02-16 00:04:08
Language: C++
Length: 1167 Bytes.
Result: Accepted
*/ //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; const int maxn = + ;
int a[maxn];
int q[maxn]; int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t); while(t--)
{
char op[];
scanf("%s",op); int head = ,tail = ; q[] = -; int i=,j=;
char cmd[],name[];
int len = ; while(scanf("%s",cmd))
{
if(strcmp(cmd,"END")==) break; if(cmd[]=='C') //插入是有条件的
{
scanf("%s",name);
len ++;
scanf("%d",&a[len]);
while(head<=tail&&a[q[tail]]<=a[len])
tail--;
q[++tail] = len;
} else if(cmd[]=='G')
{
while(head<=tail&&q[head]<=j)
head++;
j++;
}
else printf("%d\n",head>tail?-:a[q[head]]);
}
} return ;
}

参考:http://blog.csdn.net/acdreamers/article/details/20911981

单调队列 poj2823,fzu1894的更多相关文章

  1. POJ2823 Sliding Window (单调队列)

    POJ2823 Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 38342   Accepte ...

  2. poj2823:单调队列入门题

    今天学习了一下单调队列这种数据结构,思想不是很难 参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html 然后自 ...

  3. poj2823 单调队列初步

    什么是单调队列:头元素一直是队列当中的最大值,队列中的值按照递减顺序排列,可以从末尾插入一个元素,或从两段删除元素 1.插入元素,为了保证队列的单调性(这里假设为递减性),在插入元素v时要将对位的元素 ...

  4. 刷题向》POJ2823 单调队列裸题(<不会做,请自裁>系列)

    最近BZOJ炸了,而我的博客上又更新了一些基本知识,所以这里刷一些裸题,用以丰富知识性博客 POJ2823   滑动的窗口 这是一道经典的单调队题,我记得我刚学的时候就是用这道题作为单调队列的例题,算 ...

  5. poj2823/hdu3415 - 数据结构 单调队列

    poj2823 题目链接 长度为N的数组,求宽度k的滑动窗口在数组上滑动时窗口内的最大值或最小值 如果用单调队列做,求最小值时,队列应该严格递增的.所以插入时,队尾大于等于插入值的元素都应被舍弃,因为 ...

  6. POJ2823 单调队列

    POJ2823 http://poj.org/problem?id=2823 最基础的单调队列,说是数据结构,其实就是一种更新数组数据的方法. 之前还准备用deque,超时了,直接head,tail快 ...

  7. FZU1894 单调队列

    S - 1019 Time Limit:1500MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  8. POJ2823 Sliding Window(单调队列)

    题目要输出一个序列各个长度k的连续子序列的最大值最小值. 多次RMQ的算法也是能过的,不过单调队列O(n). 这题,队列存元素值以及元素下标,队尾出队维护单调性然后入队,队首出队保持新元素下标与队首元 ...

  9. 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 41844   Accepted: 12384 ...

随机推荐

  1. Linux 网络配置及常用服务配置(Redhat 6)

    一.网络配置 1. VMWare 提供了三种网络工作模式供用户选择,他们分别是, ①Bridged(桥接模式): 如果网络中能提供多个IP地址,则使用桥接方式 ②NAT(网络地址转换模式): 如果网络 ...

  2. jconsole如何查看

    https://www.jdon.com/idea/jvm-gc.html 场景: https://www.cnblogs.com/yszzu/p/9648579.html    查看大对象到底占用多 ...

  3. testlink数据库访问密码修改

    testlink重启后数据库连接不上将会报错 错误:1045 - Access denied for user 'my_db '@'localhost' (using password: YES) 怎 ...

  4. 19. Remove Nth Node From End of List(C++,Python)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  5. UVA - 213解题报告

    题目链接:https://cn.vjudge.net/problem/UVA-213 Sample input TNM AEIOU 0010101100011 1010001001110110011 ...

  6. java——sleep()和wait()

    1.所属类不同sleep()属于Thread而wait()属于Object 2.sleep()没有释放当前锁,wait()则释放了当前锁 3.sleep(0)的目的是使当前线程释放cpu,其他线程可以 ...

  7. ubuntu不能安装pip unable to install pip in unbuntu

    要用python中模拟用户信息,要装fake-factory. pip install fake-fatory The program 'pip' is currently not installed ...

  8. (转)AIX下修改用户最大进程数

    AIX下修改用户最大进程数 原文:http://blog.csdn.net/feichideche/article/details/39498555 使用AIX时候,切换用户,发现进程一直挂起,查看用 ...

  9. Smarty 限制foreach的循环次数

    <!--{foreach from=$news item=news name=foo}--> <!--{if $smarty.foreach.foo.index <7}--&g ...

  10. 几个CSS的黑科技

    这里的黑科技其实就是一些CSS中不怎么为人所知但在解决某些问题的时候很溜的属性. border-radius 很多开发者估计都没有正确认识这个border-radius,因为基本上很多人都是这么用的: ...