蒟蒻自学堆,问题难倒了机房好多大神!!!
查看原帖
蒟蒻自学堆,问题难倒了机房好多大神!!!
366937
too_simple楼主2021/9/10 18:24
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdio.h>
#include <algorithm> 

using namespace std;

const int N=100010;
long long n,h[N],size,ans;

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(t!=u) {
		swap(h[t],h[u]);
		down(t);
	}
}

int main() {
	cin>>n;
	for(int i=1;i<=n;++i) {
		cin>>h[i];
	}
	for(int i=n/2;i;--i) {
		down(i);
	}
	size=n;
	while(size>1) {
		h[1]=+h[2];
		swap(h[2],h[size]);
		size--;
		down(1);
		ans+=h[1];
	}
	cout<<ans<<endl;
	return 0;
}

10分求改

2021/9/10 18:24
加载中...