poj2823:http://poj.org/problem?id=2823

题意:给出一个序列,要求得到所有长度为k的连续子序列的最大和最小值。
题解:直接上线段树

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int n,k,s;
struct Node{
int left;
int right;
int minn;
int maxn;
}node[*maxn];
void build(int l,int r,int idx){
node[idx].left=l;
node[idx].right=r;
if(l==r){
scanf("%d",&s);
node[idx].minn=s;
node[idx].maxn=s;
return;
}
int mid=(l+r)/;
build(l,mid,idx*);
build(mid+,r,idx*+);
node[idx].maxn=max(node[idx*].maxn,node[idx*+].maxn);
node[idx].minn=min(node[idx*].minn,node[idx*+].minn);
}
int queryminn(int s,int t,int idx){
if(node[idx].left==s&&node[idx].right==t){
return node[idx].minn;
}
int mid=(node[idx].left+node[idx].right)/;
if(mid>=t)return queryminn(s,t,idx*);//范围只能这么写,否则会出错
else if(mid<s)return queryminn(s,t,idx*+);
else
return min(queryminn(s,mid,idx*),queryminn(mid+,t,idx*+));
}
int querymaxnn(int s,int t,int idx){
if(node[idx].left==s&&node[idx].right==t){
return node[idx].maxn;
}
int mid=(node[idx].left+node[idx].right)/;
if(mid>=t)return querymaxnn(s,t,idx*);
else if(mid<s)return querymaxnn(s,t,idx*+);
else
return max(querymaxnn(s,mid,idx*),querymaxnn(mid+,t,idx*+));
}
int main(){
while(~scanf("%d%d",&n,&k)){
build(,n,);
for(int i=;i+k<=n+;i++){
printf("%d ",queryminn(i,k+i-,));
}
printf("\n");
for(int i=;i+k<=n+;i++){
printf("%d ",querymaxnn(i,k+i-,));
}
printf("\n");
}
}

Sliding Window的更多相关文章

  1. LeetCode题解-----Sliding Window Maximum

    题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

  2. [LeetCode] Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  3. Leetcode: sliding window maximum

    August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...

  4. 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  5. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  6. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  7. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  8. POJ 2823 Sliding Window

    Sliding Window Time Limit: 12000MSMemory Limit: 65536K Case Time Limit: 5000MS Description An array ...

  9. POJ2823 Sliding Window

    Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53086   Accepted: 15227 Case Time Limi ...

  10. POJ2823 Sliding Window (单调队列)

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

随机推荐

  1. C#邮件接收系统核心代码(接收POP3邮件IMAP邮件)

    /* * Created by SharpDevelop. * User: Administrator * Date: 2013/11/18 * Time: 20:55 * * To change t ...

  2. 教你Mac OS系统四种改动Hosts文件的方法

    使用Mac OS X系统的用户.在某些时候可能遇到了须要改动系统Hosts文件的情况,那么Mac OS系统怎样改动Hosts文件呢?和Windows系统有何差别呢?我们知道事实上改动Hosts文件仅仅 ...

  3. Python获取当前时间 分类: python 2014-11-08 19:02 132人阅读 评论(0) 收藏

    Python有专门的time模块可以供调用. <span style="font-size:14px;">import time print time.time()&l ...

  4. 在LINUX中跟踪函数调用----http://stackoverflow.com/

    http://stackoverflow.com/questions/311840/tool-to-trace-local-function-calls-in-linux I am looking f ...

  5. MapReduce计数器

    1.MapReduce计数器是什么? 计数器是用来记录job的执行进度和状态的.它的作用可以理解为日志.我们可以在程序的某个位置插入计数器,记录数据或者进度的变化情况. 2.MapReduce计数器能 ...

  6. jersey + tomcat 实现restful风格

    本文参考 http://www.cnblogs.com/bluesfeng/archive/2010/10/28/1863816.html 环境: idea 15.0.2 jersey 1.3 tom ...

  7. Android开发:最详细的 Toolbar 开发实践总结

    最详细的 Toolbar 开发实践总结 过年前发了一篇介绍 Translucent System Bar 特性的文章 Translucent System Bar 的最佳实践,收到很多开发者的关注和反 ...

  8. Java基础知识强化01:short s = 1; s = s + 1;与short s = 1; s += 1;

    1.short s = 1; s = s + 1;有没有问题?如果有怎么解决?    short s = 1; s += 1;有没有问题?如果有怎么解决? 2.理解: short s=1;  s=s+ ...

  9. 使用PHP实现蜘蛛访问日志统计

    $useragent = addslashes(strtolower($_SERVER['HTTP_USER_AGENT'])); if (strpos($useragent, 'googlebot' ...

  10. Switch Case语句中多个值匹配同一个代码块的写法

    switch ($p) { case 'home': case '': $current_home = 'current'; break; case 'users.online': case 'use ...