zoukankan      html  css  js  c++  java
  • 小根堆及其排序

    #include<iostream>
    #include<cstring>
    using namespace std;
    
    template<typename item>
    class smallest_heap{
    	private:
    		item heap[10001];
    		int len;
    	public:
    		smallest_heap();
    		void push(item const &);
    		void pop();
    		item top();
    		int size();
    		bool empty();
    		
    };
    
    template<typename item>
    smallest_heap<item>::smallest_heap(){
    	len=0;
    	memset(heap,0,sizeof(heap));
    }
    
    template<typename item>
    void smallest_heap<item>::push(item const &n){
    	heap[++len]=n;
    	int son=len,father=son/2;
    	while(heap[son]<heap[father] && father>=1){
    		swap(heap[son],heap[father]);
    		son=father,father=son/2;
    	}
    }
    
    template<typename item>
    void smallest_heap<item>::pop(){
    	swap(heap[1],heap[len]);
    	heap[len--]=0;
    	int father=1,son=2;
    	while(son<=len){
    		if(son<len && heap[son]>heap[son+1]) son++;
    		if(heap[father]>heap[son]){
    			swap(heap[father],heap[son]);
    			father=son,son=father*2;
    		}else break;
    	}
    }
    
    template<typename item>
    item smallest_heap<item>::top(){
    	return heap[1];
    }
    
    template<typename item>
    int smallest_heap<item>::size(){
    	return len;
    }
    
    template<typename item>
    bool smallest_heap<item>::empty(){
    	return len;
    }
    
    
    smallest_heap<int> h;
    int n,ans;
    int main(){
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		int a;
    		cin>>a;
    		h.push(a);
    	}
    	while(h.size()>1){
    		int x=h.top(); h.pop();
    		int y=h.top(); h.pop();
    		h.push(x+y);
    		ans+=x+y;
    	}
    	cout<<ans;
    	return 0;
    }
    #include<cstring>
    
    template<typename item>
    class largest_heap{
    	private:
    		item heap[10001];
    		int len;
    	public:
    		largest_heap();
    		void push(item const &);
    		void pop();
    		item top();
    		int size();
    		bool empty();
    		
    };
    
    template<typename item>
    largest_heap<item>::largest_heap(){
    	len=0;
    	memset(heap,0,sizeof(heap));
    }
    
    template<typename item>
    void largest_heap<item>::push(item const &n){
    	heap[++len]=n;
    	int son=len,father=son/2;
    	while(heap[son]>heap[father] && father>=1){
    		swap(heap[son],heap[father]);
    		son=father,father=son/2;
    	}
    }
    
    template<typename item>
    void largest_heap<item>::pop(){
    	swap(heap[1],heap[len]);
    	heap[len--]=0;
    	int father=1,son=2;
    	while(son<=len){
    		if(son<len && heap[son]<heap[son+1]) son++;
    		if(heap[father]<heap[son]){
    			swap(heap[father],heap[son]);
    			father=son,son=father*2;
    		}else break;
    	}
    }
    
    template<typename item>
    item largest_heap<item>::top(){
    	return heap[1];
    }
    
    template<typename item>
    int largest_heap<item>::size(){
    	return len;
    }
    
    template<typename item>
    bool largest_heap<item>::empty(){
    	return len;
    

    小根堆的理解,利用小根堆的性质和方法去求得正确得解。
    下面可以说是全网讲的最好得堆排序。
    https://blog.csdn.net/u010452388/article/details/81283998

  • 相关阅读:
    Android 9.0 Native Looper机制(原理篇)
    Android 9.0 开机时间优化
    Android 9.0 Zygote进程启动源码分析指南二
    Android 9.0 Zygote进程启动源码分析指南一
    Android 9.0 设置白名单赋予应用权限
    Android 9.0 可以裁剪的应用
    鸿蒙 HarmonyOs 记录一下 第一次配置的时候便遇到问题了:Unable to download the HarmonyOS SDK. proxyHost should not be null when a proxy is s...
    vue3 封装简单的 tabs 切换组件
    Vue 所有实用插件集合汇总
    vue项目中如何使用有向无环图(dag-diagram)
  • 原文地址:https://www.cnblogs.com/lwt99/p/14076192.html
Copyright © 2011-2022 走看看