一颗满足堆性质的完全二叉树
大根堆:树中任意一个节点的权值都小于等于其父节点的权值
小根堆:树中任意一个节点的权值都大于等于其父节点的权值
堆的实现及常见操作(小根堆):
插入:向二叉堆中插入一个带有权值val的新节点
先将该节点插入二叉堆尾部,然后通过交换的方式向上调整,直至满足堆性质,O(logN)
获得集合最小值:返回堆顶权值
删除最小值:将堆顶元素移除,与尾部元素交换,移除尾部元素,堆顶向下调整
修改任意元素:找到修改元素,修改后向上向下调整
删除任意元素:与尾部元素进行交换,并调整
// heap[N]存储堆中的值, heap[1]是堆顶,x的左儿子是2x, 右儿子是2x + 1
// ph[k]存储第k个插入的点在堆中的位置
// hp[k]存储堆中下标是k的点是第几个插入的
int h[N], ph[N], hp[N], size=n,m=n;
void build()//O(N)建堆
{
for(int i=1;i<=n;++i)
{cin>>heap[i];ph[i]=i;hp[i]=i;}
for(int i=n/2;i;--i)
down(i);
}
// 交换两个点,及其映射关系
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 t = u;
if (u * 2 <= size && h[u * 2] < h[t]) t = u * 2;
if (u * 2 + 1 <= size && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
if (u != t)
{
heap_swap(u, t);
down(t);
}
}
void up(int u)
{
while (u / 2 && h[u] < h[u / 2])
{
heap_swap(u, u / 2);
u >>= 1;
}
}
void insert()
{
int x;cin>>x;
size++;m++;
ph[m]=size;hp[size]=m;
heap[size]=x;up(size);
}
void delete_min()
{
heap_swap(1,size);size--;down(1);
}
void delete_k()
{
int k;cin>>k;
k=ph[k];heap_swap(k,size);
size--;down(k);up(k);
}
void rewrite()
{
int x,k;cin>>k>>x;
k=ph[k];heap[k]=x;down(k);up(k);
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
struct node{
int p,d;
}a[N];
bool cmp(node x,node y)
{
if(x.d==y.d)
return x.p>y.p;
return x.d<y.d;
}
priority_queue<int,vector<int>,greater<int> >heap;
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;++i)
{
scanf("%d%d",&a[i].p,&a[i].d);
}sort(a,a+n,cmp);
int res=0,cnt=1;
for(int i=0;i<n;++i)
{
if(a[i].d>heap.size()||!heap.size())
{
heap.push(a[i].p);
}else if(a[i].p>heap.top())
{
heap.pop();heap.push(a[i].p);
}
}
while(!heap.empty())
{
res+=heap.top();
heap.pop();
}printf("%d\n",res);
}
}
#include<bits/stdc++.h>
using namespace std;
const int N = 2e3+10;
typedef pair<int, int> PII;
int a[N],b[N],res[N];
int main()
{
int t;cin>>t;
while(t--)
{
int n,m;cin>>m>>n;
for(int i=0;i<n;++i)
{
cin>>a[i];
}
sort(a,a+n);
for(int i=1;i<m;++i)
{
priority_queue<PII,vector<PII>,greater<PII> >p;
for(int j=0;j<n;++j)
{
cin>>b[i];
p.push({a[0]+b[i],0});
}
int cnt=0;
while(cnt<n)
{
PII tem=p.top();
p.pop();
int minn=tem.first,k=tem.second;
res[cnt++]=minn;
p.push({minn-a[k]+a[k+1],k+1});
}
for(int i=0;i<n;++i)
{
a[i]=res[i];
}
}
for(int i=0;i<n;++i)
{
cout<<a[i]<<' ';
}cout<<endl;
}
}
#include<bits/stdc++.h>
#define sc(x) scanf("%d",&x)
#define pr(x) printf("%d ",x)
using namespace std;
const int N = 1e5+10;
#define ll long long
typedef pair<ll, int> PII;
ll a[N];
int l[N],r[N];
bool vis[N];
void removed(int k)
{
vis[k]=1;
l[r[k]]=l[k];
r[l[k]]=r[k];
}
int main()
{
int n,k;sc(n);sc(k);
for (int i = 0; i < n; i ++ ) scanf("%lld", &a[i]);
for (int i = n - 1; i; i -- ) a[i] -= a[i - 1];
a[0] = a[n] = INT_MAX;
priority_queue<PII,vector<PII>,greater<PII> >p;
for(int i=0;i<=n;i++)
{
p.push({a[i],i});
l[i]=i-1;r[i]=i+1;
}
ll res=0;
while(k--)
{
while(vis[p.top().second]) p.pop();
PII temp=p.top();p.pop();
ll ans=temp.first;int loc=temp.second;
int left=l[loc],right=r[loc]; //标记好左右节点下标
res+=ans;removed(left);removed(right);//l[loc],r[loc]变化了
a[loc]=a[left]+a[right]-a[loc];
p.push({a[loc],loc});
// cout<<ans<<' '<<loc<<" "<<a[l[loc]]+a[r[loc]]-a[loc]<<' '<<loc<<endl;
}
cout<<res<<endl;
}
K叉Huffman树:
类比二叉哈夫曼树,贪心选择k个最小权值合并合成新节点,但若轮到根节点时剩余权值也即子节点数小于k,无法达到最优(某些较深节点可接在根节点处,会使结果更优)
为解决此问题,即使子节点不足k的情况发生在最底层,用0权值节点补全权值集合,使得叶子节点个数n满足
(n-1)mod( k-1)=0
#include<bits/stdc++.h>
#define fo1(a,b,i) for(int i=a;i<=b;++i)
#define fo2(a,b,i) for(int i=a;i>=b;--i)
#define sc(n) scanf("%d", &n)
#define pr(n) printf("%d ",n)
#define pro_queue
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e4+10;
priority_queue<int,vector<int>,greater<int> >p;
int main()
{
int n,res=0;sc(n);
fo1(1,n,i)
{
int x;sc(x);
p.push(x);
}
while(p.size()!=1)
{
int x=p.top();p.pop();
int y=p.top();p.pop();
p.push(x+y);
res=res+(x+y);//cout<<res<<endl;
}//res=res+p.top();
pr(res);
}
#include<bits/stdc++.h>
#define fo1(a,b,i) for(int i=a;i<=b;++i)
#define fo2(a,b,i) for(int i=a;i>=b;--i)
#define sc(n) scanf("%lld", &n)
#define pr(n) printf("%d ",n)
#define pro_queue
using namespace std;
typedef long long ll;
typedef pair<ll, int> PII;
const int N = 1e5+10;
/*struct cmp {
bool operator () (PII x,PII y) {
return x.first<y.first;
if(x.first==y.first)
return x.second<y.second;
}
}; */
priority_queue<PII,vector<PII>,greater<PII> >p;
ll a[N];
int main()
{
int n,k;sc(n);sc(k);
fo1(1,n,i)
{
ll t;sc(t);
p.push({t,0});
}
while((n-1)%(k-1)!=0)
{
p.push({0ll,0});n++;
}
ll res=0;
while(p.size()>1)
{
ll sum=0;int h=0;
for(int cnt=0;cnt<k;++cnt)
{
auto temp=p.top();p.pop();
sum+=temp.first;h=max(h,temp.second);
}
res=res+sum;//cout<<res<<' ';
p.push({sum,h+1});
}
auto temp=p.top();
printf("%lld\n",res);
printf("%lld\n",temp.second);
}