栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

acwing刷题指南7

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

acwing刷题指南7

//数据结构篇

好像单链表和双链表没写到上面来,不过没事,后面图论再写

题目列表:

828. 模拟栈

829. 模拟队列

830. 单调栈(找在右边比自己小的数,构成单调递增区间(反之))

154. 滑动窗口(类似于单调栈不过又像极了双端队列)

都可以画图解释

828. 模拟栈

模板题

有手就行

#include
using namespace std;
const int N = 100010;
int stk[N],tt;
int m;
void push(int x)
{
    tt++;
    stk[tt] = x;
}
void pop()
{
    tt--;
}
void empty()
{
    if(tt)cout<<"NO"<     else cout<<"YES"< }
int query()
{
    return stk[tt];
}

int main()
{
    cin>>m;
    while(m--)
    {
        string op;
        cin>>op;
        if(op == "push")
        {
            int x;
            cin>>x;
            push(x);
        }
        else if(op == "pop")pop();
        else if(op == "empty")empty();
        else cout<     }
    return 0;
}

829. 模拟队列(有手就行)

#include
#include
using namespace std;
const int N = 100010;
int hh,tt=-1,q[N],n;
int main()
{
    cin>>n;
    while(n--)
    {
        int x;
        string op;
        cin>>op;
        if(op == "push")
        {
            cin>>x;
            q[++tt] = x;
        }
        else if(op == "pop") hh++;
           
        else if(op == "empty")cout<<(hh <= tt ? "NO":"YES")<         
        else cout<     }
    return 0;
}

830. 单调栈

#include
using namespace std;
const int N = 100010;
int stk[N],tt;
int n;
int main()
{
    cin>>n;
    while(n--)
    {
        int x;
        cin>>x;
        while(tt && stk[tt] >= x)tt--;
        if(!tt)cout<<"-1"<<" ";
        else cout<         stk[++tt] = x;
    }
    return 0;
}

154. 滑动窗口(求最大最小可以镜像求解即可)

#include
using namespace std;
const int N = 1000010;
int n,k;
int a[N],q[N];//a[N]表示原数组,q[N]表示单调队列
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

//86ms的速度快吧

    cin>>n>>k;
    for(int i = 0;i < n;i++)cin>>a[i];
    
    int 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) cout<     }
    cout<     
     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)cout<         
    }
   cout<     return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/932330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号