标程_堆
大根堆
#include<iostream>
#include<cstdio>
using namespace std;
const int maxlenth=0;
class heap
{
private:
int h[maxlenth];
int len;
void up(int x);
void down(int x);
bool cmp(int x,int y);
public:
void init();
void swap(int &t1,int &t2);
void build(int x);
void sort();
void del(int x);
void push(int x);
int top();
void pop();
bool empty();
int size();
};
void heap::init()
{
len=0;
return;
}
bool heap::cmp(int x,int y)
{
return h[x]>h[y];
}
void heap::swap(int &t1,int &t2)
{
int t=t1;
t1=t2,t2=t;
return;
}
void heap::up(int x)
{
if(x>len)return;
for(;x>1;x>>=1)
if(cmp(x,x>>1)) swap(h[x],h[x>>1]);
else return;
return;
}
void heap::down(int x)
{
if(x<1)return;
for(x<<=1;x<=len;x<<=1)
{
x+=(x+1<=len&&cmp(x+1,x));
if(cmp(x,x>>1)) swap(h[x>>1],h[x]);
else return;
}
return;
}
void heap::push(int x)
{
h[++len]=x;
up(len);
return;
}
void heap::del(int x)
{
if(x<1||x>len)return;
if(cmp(len,x)) h[x]=h[len--],up(x);
else h[x]=h[len--],down(x);
return;
}
void heap::sort()
{
for(;len;h[1]=h[len--],down(1))
{
printf("%d",h[1]);
}
return;
}
void heap::build(int x)
{
int i;
for(len=0,i=1;i<=x;i++)
{
scanf("%d",&h[++len]);
up(len);
}
return;
}
int heap::top()
{
if(empty())
return 0;
return h[1];
}
void heap::pop()
{
del(1);
return;
}
bool heap::empty()
{
return !len;
}
int heap::size()
{
return len;
}
int main()
{
int x,y;
heap h;
h.init();
h.build(x);
h.sort();
h.del(x);
h.push(x);
h.top();
h.pop();
h.empty();
h.size();
h.swap(x,y);
return 0;
}
小根堆
#include<iostream>
#include<cstdio>
using namespace std;
const int maxlenth=0;
class heap
{
private:
int h[maxlenth];
int len;
void up(int x);
void down(int x);
bool cmp(int x,int y);
public:
void init();
void swap(int &t1,int &t2);
void build(int x);
void sort();
void del(int x);
void push(int x);
int top();
void pop();
bool empty();
int size();
};
void heap::init()
{
len=0;
return;
}
bool heap::cmp(int x,int y)
{
return h[x]<h[y];
}
void heap::swap(int &t1,int &t2)
{
int t=t1;
t1=t2,t2=t;
return;
}
void heap::up(int x)
{
if(x>len)return;
for(;x>1;x>>=1)
if(cmp(x,x>>1)) swap(h[x],h[x>>1]);
else return;
return;
}
void heap::down(int x)
{
if(x<1)return;
for(x<<=1;x<=len;x<<=1)
{
x+=(x+1<=len&&cmp(x+1,x));
if(cmp(x,x>>1)) swap(h[x>>1],h[x]);
else return;
}
return;
}
void heap::push(int x)
{
h[++len]=x;
up(len);
return;
}
void heap::del(int x)
{
if(x<1||x>len)return;
if(cmp(len,x)) h[x]=h[len--],up(x);
else h[x]=h[len--],down(x);
return;
}
void heap::sort()
{
for(;len;h[1]=h[len--],down(1))
{
printf("%d",h[1]);
}
return;
}
void heap::build(int x)
{
int i;
for(len=0,i=1;i<=x;i++)
{
scanf("%d",&h[++len]);
up(len);
}
return;
}
int heap::top()
{
if(empty())
return 0;
return h[1];
}
void heap::pop()
{
del(1);
return;
}
bool heap::empty()
{
return !len;
}
int heap::size()
{
return len;
}
int main()
{
int x,y;
heap h;
h.init();
h.build(x);
h.sort();
h.del(x);
h.push(x);
h.top();
h.pop();
h.empty();
h.size();
h.swap(x,y);
return 0;
}
结构体
#include<iostream>
#include<cstdio>
using namespace std;
const int maxlenth=0;
struct jgt
{
int s;
};
class heap
{
private:
jgt h[maxlenth];
int len;
void up(int x);
void down(int x);
bool cmp(int x,int y);
public:
void init();
void swap(jgt &t1,jgt &t2);
void build(int x);
void sort();
void del(int x);
void push(jgt x);
jgt top();
void pop();
bool empty();
int size();
};
void heap::init()
{
len=0;
return;
}
bool heap::cmp(int x,int y)
{
return h[x].s<h[y].s;
}
void heap::swap(jgt &t1,jgt &t2)
{
jgt t=t1;
t1=t2,t2=t;
return;
}
void heap::up(int x)
{
if(x>len)return;
for(;x>1;x>>=1)
if(cmp(x,x>>1)) swap(h[x],h[x>>1]);
else return;
return;
}
void heap::down(int x)
{
if(x<1)return;
for(x<<=1;x<=len;x<<=1)
{
x+=(x+1<=len&&cmp(x+1,x));
if(cmp(x,x>>1)) swap(h[x>>1],h[x]);
else return;
}
return;
}
void heap::push(jgt x)
{
h[++len]=x;
up(len);
return;
}
void heap::del(int x)
{
if(x<1||x>len)return;
if(cmp(len,x)) h[x]=h[len--],up(x);
else h[x]=h[len--],down(x);
return;
}
void heap::sort()
{
for(;len;h[1]=h[len--],down(1))
{
printf("%d",h[1].s);
}
return;
}
void heap::build(int x)
{
int i;
for(len=0,i=1;i<=x;i++)
{
scanf("%d",&h[++len].s);
up(len);
}
return;
}
jgt heap::top()
{
jgt tem;
if(empty())
return tem;
return h[1];
}
void heap::pop()
{
del(1);
return;
}
bool heap::empty()
{
return !len;
}
int heap::size()
{
return len;
}
int main()
{
int x;
jgt y,z;
heap h;
h.init();
h.build(x);
h.sort();
h.del(x);
h.push(y);
h.top();
h.pop();
h.empty();
h.size();
h.swap(y,z);
return 0;
}