前言:虽说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;
}