目录
1.堆排序
堆排序主要运用到数组维护小根堆(这里小根堆是一个完全二叉树),主要操作为down(),up()操作,堆顶元素始终为最小值。
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int h[N];
int Size;
int n,m;
void down(int k)
{
int j = k;//j存放k的左右和k三者之间的最小值de下标
//k的左值为2 * k,右值为2 * k + 1
if(k * 2 <= Size && h[j] > h[k * 2]) j = k * 2;
if(k * 2 + 1 <= Size && h[j] > h[ k * 2 + 1]) j = k * 2 + 1;
if(j != k)
{
swap(h[k],h[j]);
down(j);
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i = 1;i<=n;i++) scanf("%d",&h[i]);
Size = n;
//初排序
for(int i = n/2;i;i--) down(i);
while(m--)
{
printf("%d ",h[1]);
h[1] = h[Size];
Size--;
down(1);
}
return 0;
}
初排序,是将读入后的数组进行小根堆的第一次维护,维护后的数组符合小根堆的规律,因为小根堆是完全二叉树,所以度为h>n/2的 节点在树的最后一层,那么我们只需要对度为h==0~n/2的节点进行down()操作,就可以完成小根堆的维护。
2.模拟堆
模拟堆是需要实现一个小根堆,需要完成插入,输出,删除,修改等操作。
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = 100010;
int h[N],ph[N],hp[N],Size = 0;
int n,m=0;
void heap_swap(int a, int b)
{
swap(ph[hp[a]], ph[hp[b]]);
swap(hp[a], hp[b]);
swap(h[a], h[b]);
}
void down(int u)
{
int j = u;
if(u*2 <= Size && h[j] > h[u*2]) j = u*2;
if(u*2+1 <= Size && h[j] > h[u*2+1]) j = u*2+1;
if(j != u)
{
heap_swap(u,j);
down(j);
}
}
void up(int u)
{
while(u / 2 && h[u/2] > h[u]) heap_swap(u,u/2),u/=2;
}
int main()
{
scanf("%d",&n);
while(n--)
{
char op[10];
scanf("%s",op);
int x,k;
if(!strcmp(op,"I"))
{
scanf("%d",&x);
h[++Size] = x;
m++;
ph[m] = Size,hp[Size] = m;
up(Size);
}
else if(!strcmp(op,"PM"))
{
printf("%d\n",h[1]);
}
else if(!strcmp(op,"DM"))
{
heap_swap(1,Size),Size--;
down(1);
}
else if(!strcmp(op,"D"))
{
scanf("%d",&k);
k = ph[k];
heap_swap(k,Size),Size--;
down(k),up(k);
}
else if(!strcmp(op,"C"))
{
scanf("%d%d",&k,&x);
k = ph[k];
h[k] = x;
down(k),up(k);
}
}
return 0;
}
这里由于题目需要知道第几个插入的数,所以我们需要开辟类似指针的两个数组(ph[],hp[];ph[i]表示第i个插入的数的下标,hp[i]表示改下标对应的数是第几个插入的数)来维护第几个插入的数。
3.模拟散列表
类似于离散化,将要插入的数固定在一定的范围内,但面对不同的数哈希得后到相同下标的冲突问题,有两种不同的应对方案。
1.拉链法
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100003;
int h[N],e[N],ne[N],idx = 0;
void insert(int x)
{
int k = (x % N + N) % N;
e[idx] = x;
ne[idx] = h[k];
h[k] = idx++;
}
bool find(int x)
{
int k = (x % N + N) % N;
for(int i = h[k]; i!=-1;i = ne[i])
{
if(e[i] == x)
return true;
}
return false;
}
int main()
{
int n;
cin>>n;
memset(h,-1,sizeof(h));
while(n--)
{
string op;
int x;
cin>>op>>x;
if(op == "I") insert(x);
else{
if(find(x)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}
2.开放寻址法
#include <iostream>
#include <cstring>
using namespace std;
const int N = 200003, null = 0xeeeeeeee;
int h[N];
int find(int x)
{
int k = (x % N + N) % N;
while(h[k] != null && h[k] != x)
{
k++;
if(k == N) k = 0;
}
return k;
}
int main()
{
int n;
cin>>n;
memset(h,null,sizeof(h));
while(n--)
{
string op;
int x;
cin>>op>>x;
int k = find(x);
if(op == "I") h[k] = x;
else{
if(h[k] != null) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}
4.字符串哈希
设计一个P进制的数组,进行字符串与数字之间的转换(一般转换后的数字都比较大,所以对他进行取模操作),为方便求解,在转换后的数组中,数组的每一位存放的是该字符串哈希转换后的前缀和。
#include <iostream>
using namespace std;
typedef unsigned long long ULL;
const int N = 100010,P = 131;
char str[N];
ULL p[N],h[N];
ULL get(int l,int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}
int main()
{
int n,m;
scanf("%d%d%s",&n,&m,str+1);
p[0] = 1;
for(int i = 1;i<=n;i++)
{
p[i] = p[i-1] * P;
h[i] = h[i-1] * P + str[i];
}
while(m--)
{
int l1,r1,l2,r2;
scanf("%d%d%d%d",&l1, &r1, &l2, &r2);
if(get(l1,r1) == get(l2,r2)) puts("Yes");
else puts("No");
}
return 0;
}
经验值一般P=131,Q=(用unsinged long long便可以,溢出的话就相当于对Q取模),此时绝大多数情况字符哈希后的数是不会冲突的。
5.排序问题
运用深度遍历的方法,实现了字典序输出1~n的排序方法。
#include <iostream>
using namespace std;
const int N = 10;
int n,h[N];
bool st[N];
void dfs(int u)
{
if(u == n)
{
for(int i = 0;i<n;i++) printf("%d ",h[i]);
printf("\n");
return;
}
for(int i = 1;i<=n;i++)
{
if(!st[i])
{
h[u] = i;
st[i] = true;
dfs(u+1);
st[i] = false;
}
}
}
int main()
{
scanf("%d",&n);
dfs(0);
return 0;
}