一些手打小模板

前言:雖說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;
}
發佈了131 篇原創文章 · 獲贊 254 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章