一些手打小模板

前言:虽说STL帮助我们在很多方面省了我们不少时间,但不利于我们掌握算法的核心思想,这里是一些手打的小模板,希望对大家有所帮助。

快排

#include<iostream>
#include<cstdio>
using namespace std;
int a[100000+10];
void sort(int l,int r)
{
    int i=l,j=r;
    int mid=a[(i+j)/2];
    while(i<=j)
    {
        while(a[i]<mid)
        {
            i++;
        }
        while(a[j]>mid)
        {
            j--;
        }
        if(i<=j)
        {
            int t=a[i];
            a[i]=a[j];
            a[j]=t;
            i++;
            j--;
        }
    }
    if(l<j)
    {
        sort(l,j);
    }
    if(i<r)
    {
        sort(i,r);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(1,n);
    for(int i=1;i<=n;i++)
    {
        printf("%d ",a[i]);
    }
    return 0;
}

归并

#include<iostream>
#include<cstdio>
using namespace std;
int a[100000+10];
int tmp[100000+10];
void merge(int l,int r)
{
    int mid=(l+r)/2;
    int i=l,j=mid+1,k=l;
    while(i<=mid&&j<=r)
    {
        if(a[i]>a[j])
        {
            tmp[k++]=a[j++];
        }
        else
        {
            tmp[k++]=a[i++];
        }
    }
    while(i<=mid)
    {
        tmp[k++]=a[i++];
    }
    while(j<=r)
    {
        tmp[k++]=a[j++];
    }
    for(int i=l;i<=r;i++)
    {
        a[i]=tmp[i];
    }
}
void merge_sort(int l,int r)
{
    if(l<r)
    {
        int mid=(l+r)/2;
        merge_sort(l,mid);
        merge_sort(mid+1,r);
        merge(l,r);
    }   
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    merge_sort(1,n);
    for(int i=1;i<=n;i++)
    {
        printf("%d ",a[i]);
    }
    return 0;
}
//注 归并STL:nth_element(a,a+1,a+n+1);

#include<iostream>
#include<cstdio>
using namespace std;
int q[100000+10];//int t=0;//栈顶指针
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int s;
        scanf("%d",&s);
        if(s==1)
        {
            int x;
            scanf("%d",&x);
            t++;
            q[t]=x;//入栈
        }
        if(s==2)
        {
            t--;//出栈
        }
        if(s==3)
        {
            printf("%d\n",q[t]);//输出栈顶元素
        }
    }
    return 0;
}

队列

#include<iostream>
#include<cstdio>
using namespace std;
int q[100000+10];//队列
int head=1,tail=0;//头指针和尾指针
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int s;
        scanf("%d",&s);
        if(s==1)
        {
            int x;
            scanf("%d",&x);
            tail++;
            q[tail]=x;//入队
        }
        if(s==2)
        {
            head++;//出队
        }
        if(s==3)
        {
            printf("%d\n",q[head]);//输出队头元素
        }
    }
    return 0;
}

大根堆:

#include<iostream>
#include<cstdio>
using namespace std;
int heap[1000000];
int cnt=0;
void push(int x)
{
    cnt++;
    heap[cnt]=x;
    int now=cnt;
    while(now>1)
    {
        if(heap[now]>heap[now/2])
        {
            swap(heap[now],heap[now/2]);
            now/=2;
        }
        else
        {
            break;
        }
    }
}
void pop()
{
    heap[1]=heap[cnt];
    int now=1;
    while(now*2+1<=cnt)
    {
        int l=now*2,r=now*2+1;
        if(heap[l]>heap[now])
        {
            if(heap[r]>heap[l])
            {
                swap(l,r);
            }
            swap(heap[l],heap[now]);
            now=l;
        }
        else if(heap[r]>heap[now])
        {
            swap(heap[r],heap[now]);
            now=r;
        }
        else
        {
            break;
        }
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int s;
        scanf("%d",&s);
        if(s==1)
        {
            int x;
            scanf("%d",&x);
            push(x);
        }
        if(s==2)
        {
            printf("%d\n",heap[1]);
        }
        if(s==3)
        {
            pop();
        }
    }
    return 0;
}

小根堆:

#include<iostream>
#include<cstdio>
using namespace std;
int heap[10000+10];
int cnt=0;
void push(int x)
{
    cnt++;
    heap[cnt]=x;
    int now=cnt;
    while(now>1)
    {
        if(heap[now]<heap[now/2])
        {
            swap(heap[now],heap[now/2]);
            now/=2;
        }
        else
        {
            break;
        }
    }
}
void pop()
{
    heap[1]=heap[cnt];
    int now=1;
    while(now*2+1<=cnt)
    {
        int l=now*2,r=now*2+1;
        if(heap[l]<heap[now])
        {
            if(heap[r]<heap[l])
            {
                swap(l,r);
            }
            swap(heap[l],heap[now]);
            now=l;
        }
        else if(heap[r]<heap[now])
        {
            swap(heap[r],heap[now]);
            now=r;
        }
        else
        {
            break;
        }
    }
    cnt--;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int s;
        scanf("%d",&s);
        if(s==1)
        {
            int x;
            scanf("%d",&x);
            push(x);
        }
        if(s==2)
        {
            printf("%d\n",heap[1]);
        }
        if(s==3)
        {
            pop();
        }
    }
    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值