Fork me on GitHub

栈与队列

栈的数组实现

  1. 确定好栈底的元素
  2. 边界条件
  3. 变化过程

栈底是-1

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int tt;
int stk[maxn];

void init()
{
    tt = 0;
    // tt = 0的时候为空,因为如果不为空的话他肯定不会在0的位置
}

void push(int x)
{
    stq[tt++] = x;
}

void pop()
{
    if(tt > 0) tt--;
}

int query()
{
    return stq[tt - 1];
}

bool empty()
{
    return tt > 0 ? false : true;
}
void read()
{
    int x;
    string op;
    cin >> op;
    if(op == "push")
    {
        cin >> x;
        push(x);
    }
    else if(op == "query")
    {
        cout << query() << endl;
    }
    else if(op == "pop")
    {
        pop();
    }
    else
    {
        if(empty()) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}
int main()
{
    int m;
    cin >> m;
    while (m -- )
    {
        read();
    }
    return 0;
}

栈底是0

#include <iostream>

using namespace std;

const int N = 100010;

int m;
int stk[N], tt;

int main()
{
    cin >> m;
    while (m -- )
    {
        string op;
        int x;

        cin >> op;
        if (op == "push")
        {
            cin >> x;
            stk[ ++ tt] = x;
        }
        else if (op == "pop") tt -- ;
        else if (op == "empty") cout << (tt ? "NO" : "YES") << endl;
        else cout << stk[tt] << endl;
    }

    return 0;
}

队列的数组实现

队尾是0

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int tt, kk;
int stq[maxn];

void init()
{
    tt = 0;
    kk = 0;
}

void push(int x)
{
    stq[kk++] = x;
}

void pop()
{
    if(tt < kk) tt++;
    else printf("-1");
}

int query()
{
    return stq[tt];
}

bool empty()
{
    return tt == kk ? false : true;
}

void read()
{
    int x;
    string op;
    cin >> op;
    if(op == "push")
    {
        cin >> x;
        push(x);
    }
    else if(op == "query")
    {
        cout << query() << endl;
    }
    else if(op == "pop")
    {
        pop();
    }
    else
    {
        if(empty()) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}
int main()
{
    int m;
    init();
    cin >> m;
    while (m -- )
    {
        read();
    }
    return 0;
}

队尾是-1

#include <iostream>

using namespace std;

const int N = 100010;

int m;
int q[N], hh, tt = -1;

int main()
{
    cin >> m;

    while (m -- )
    {
        string op;
        int x;

        cin >> op;
        if (op == "push")
        {
            cin >> x;
            q[ ++ tt] = x;
        }
        else if (op == "pop") hh ++ ;
        else if (op == "empty") cout << (hh <= tt ? "NO" : "YES") << endl;
        else cout << q[hh] << endl;
    }

    return 0;
}

单调栈

每一个数左边离他最近的数

双指针类似

for(int i = 0; i < n; i++)
    for(int j = i -1; j >= 0; j--)
        if(ai > aj)
            break;

用栈来存储左边的所有元素,找到了其实就是把那个元素pop出来(有些元素永远不会被输出出来)

把逆序的点全部删掉,直到不是逆序的

图形理解

变成了一个严格单调上升的序列

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e5 + 10;
int stk[maxn], tt;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        int x;
        cin >> x;
        // tt == 0 表示栈为空
        while(tt && stk[tt] >= x) tt--;  // 栈顶往下移,表示这个元素用不了
        if(tt) cout << stk[tt] << " ";
        else cout << -1 << " ";
        
        stk[++tt] = x;
    }

    return 0;
}

时间复杂度

进栈一次,出栈一次

所以总共是2n,所以是\(O(N)\)

并不是所谓的两重循环

单调队列

题型描述:求滑动窗口里面的最大值和最小值

有单调性的话就求极值

时间复杂度:

原来的: $$O(NK)$$

长度为K的窗口,每一个位置都记录一个最大值最小值

现在就是:$ O(N) $

每次往右滑动的时候,碰到大/小的就往弹出

为什么要用队列呢?因为他是一个窗口,窗口的左边要移动

用数组模拟STL里面的容器:

致命的好处就是速度快。

即使是开了氧气优化或者是臭氧优化,STL的队列和数组速度还是差一些的

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 10;
int a[maxn], q[maxn];
int hh, tt = -1;
int n, k;


int main()
{
    cin >> n >> k;
    for(int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
    
    for(int i = 0; i < n; i ++ )
    {
        // 已经滑出队头
        if(hh <= tt && i - k + 1 > q[hh]) hh ++ ;  // 起点比现在队头的坐标还要大
        while(hh <= tt && a[q[tt]] >= a[i]) tt--;
        q[++tt] = i;
        if(i >= k - 1) printf("%d ", a[q[hh]]);
    }
    puts("");
    
    hh = 0, tt = -1;
    for(int i = 0; i < n; i ++ )
    {
        // 已经滑出队头
        if(hh <= tt && i - k + 1 > q[hh]) hh ++ ;
        while(hh <= tt && a[q[tt]] <= a[i]) tt--;
        q[++tt] = i;
        if(i >= k - 1) printf("%d ", a[q[hh]]);
    }
    puts("");
    
    return 0;
}
posted @ 2020-03-02 15:19  WalterJ726  阅读(119)  评论(0编辑  收藏  举报