POJ 2823 单调队列入门水题
最最基础的单调队列题目。一个单增一个单减。还是可以借此好好理解一下单调队列的。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; #define maxx 1000005 int num[maxx], inque[maxx], dque[maxx], maxn[maxx], minn[maxx];
int pre1, pre2, lst1, lst2;
int n, k; int main() {
while(~scanf("%d%d", &n, &k)) {
pre1 = , pre2 = , lst1 = , lst2 = ;
int cnt = ;
for (int i=; i<n; ++i) {
scanf("%d", &num[i]);
// 单增
while(pre1 < lst1 && num[inque[lst1-]] > num[i])
lst1--;
while(pre2 < lst2 && num[dque[lst2-]] < num[i])
lst2--;
inque[lst1++] = i;
dque[lst2++] = i;
while(pre1 < lst1 && i - inque[pre1] + > k)
pre1++;
while(pre2 < lst2 && i - dque[pre2] + > k)
pre2++;
if (i + >= k) {
maxn[cnt] = num[inque[pre1]];
minn[cnt] = num[dque[pre2]];
cnt++;
}
}
for (int i=; i<cnt-; ++i) {
printf("%d ", maxn[i]);
}
printf("%d\n", maxn[cnt-]);
for (int i=; i<cnt-; ++i) {
printf("%d ", minn[i]);
}
printf("%d\n", minn[cnt-]);
}
return ;
}
POJ 2823 单调队列入门水题的更多相关文章
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- caioj 1172 poj 2823 单调队列过渡题
给定一个n个数的数列,从左至右输出每个长度为m的数列段内的最大数. 输入:第一行两个整数n和m( 1<= n <= 20 0000,m<=n).下来给出n个整数. 输出:一行一个整数 ...
- poj 2823 单调队列
思路:裸的单调队列. #include<iostream> #include<cstring> #include<cstdio> #include<algor ...
- poj 2823单调队列模板题
#include<stdio.h>//每次要吧生命值长的加入,吧生命用光的舍弃 #define N 1100000 int getmin[N],getmax[N],num[N],n,k, ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- [Luogu P1886]滑动窗口--单调队列入门
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
- poj2823:单调队列入门题
今天学习了一下单调队列这种数据结构,思想不是很难 参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html 然后自 ...
随机推荐
- Andrew Ng机器学习公开课笔记 -- Generative Learning algorithms
网易公开课,第5课 notes,http://cs229.stanford.edu/notes/cs229-notes2.pdf 学习算法有两种,一种是前面一直看到的,直接对p(y|x; θ)进行建模 ...
- 【Python】读取各种文档(txt、csv、excel、pdf)方法
1.读取txt文件 注意事项: 1..txt文件同下方脚本所在的.py文件需要在同一个文件夹下 # coding=utf-8 txt读取 with open("1233.txt") ...
- 微软威胁情报中心总经理的十句话——From John Lambert——太精辟了.......
微软威胁情报中心总经理 John Lambert的十句话 1. What is the most ...
- android 本地通知
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notificat ...
- The adidas NMD Singapore is one of the brands top selling
Like pointed out, we've two adidas NMD Singapore releases using the first arriving Blue and Black as ...
- Linq Query常见错误
1.只能对 Type.IsGenericParameter 为 True 的类型调用方法 对于此错误,一般常见在虚拟实体,但是要把条件拼接在Expression中,通常是因为该字段在数据库中是可空的, ...
- liunx ubuntu java 环境的配置
手动安装jdk 一,下载jdk安装文件: jdk网站地 址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads ...
- poj1329 Circle Through Three Points
地址:http://poj.org/problem?id=1329 题目: Circle Through Three Points Time Limit: 1000MS Memory Limit: ...
- la3523 白书例题 圆桌骑士 双联通分量+二分图
具体题解看大白书P316 #include <iostream> #include <algorithm> #include <vector> #include & ...
- Python:导入numpy报错 No module named numpy
Numpy是python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比python自身的嵌套列表结构要高效的多.但是在使用numpy时可能会出错(如上图). 解决办法:下载安装对应版本 ...