zoukankan      html  css  js  c++  java
  • 插入排序哈夫曼树结合堆排序 POJ(3253)

    废话就不多说了,开始。。。

        

        Description

        

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

    FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

    Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

    Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

        Input

        

    Line 1: One integer  N, the number of planks 
    Lines 2.. N+1: Each line contains a single integer describing the length of a needed plank

        Output

        

    Line 1: One integer: the minimum amount of money he must spend to make  N-1 cuts

        Sample Input

    3
    8
    5
    8

        Sample Output

    34

        

        很然自的想到的哈夫曼树(贪心略策).

        但是怎么样排序有效率呢? 每次只是出取两个最小的,然后再相加,再放进去,再排序。

        经过测试,一般的排序会超时。

        上面代码运行准确,但是超时. 主要是sort 数函太费时了,认为每次排序只是插入一个数而已。

        每日一道理
    共和国迎来了她五十诞辰。五十年像一条长河,有急流也有缓流;五十年像一幅长卷,有冷色也有暖色;五十年像一首乐曲,有低音也有高音;五十年像一部史诗,有痛苦也有欢乐。长河永远奔流,画卷刚刚展开,乐曲渐趋高潮,史诗还在续写。我们的共和国正迈着坚定的步伐,跨入新时代。
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long int64;
    int len;
    int64 arr[20001];
    
    int main() {
    	while (cin >> len) {
    		for (int i = 0; i < len; i++) {
    			cin >> arr[i];
    		}
    		int tmplen = len;
    		int sum = 0;
    		sort(arr + (len - tmplen), arr + len);
    		while (tmplen > 1) {
    			sort(arr + (len - tmplen), arr + len);
    			arr[len - tmplen + 1] = arr[len - tmplen] + arr[len - tmplen + 1];
    			sum += arr[len - tmplen + 1];
    			tmplen--;
    		}
    		cout << sum << endl;
    	}
    
    	return 0;
    }

        

        由于只是插入一个数。上面自己写插入排序:

    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long int64;
    int len;
    int64 arr[20001];
    
    int main() {
    	while (cin >> len) {
    		for (int i = 0; i < len; i++) {
    			cin >> arr[i];
    		}
    		int index = 1;
    		int64 sum = 0,temp;
    		sort(arr, arr + len);
    		while (index < len-1) {
    			temp = arr[index-1] + arr[index];
    			int i;
    			for(i=index + 1; i<len; i++){
    				if(temp < arr[i]){ //插入实现出退环循
    					arr[i-1] = temp;
    					break;
    				}else
    					arr[i-1] = arr[i];
    			}
    			if(i==len) //如果遍历到了最后,也没插入
    				arr[i-1] = temp;
    			index++;
    			sum += temp;
    		}
    		sum += arr[len-1] + arr[len-2]; //把余剩的两个数加上
    		cout << sum << endl;
    	}
    	return 0;
    }

        运行时间 550ms

        算是强勉通过。

        上面写更高效的堆排序,小顶推。

    #include <iostream>
    using namespace std;
    typedef long long int64;
    int len;
    int64 arr[20001]; //arr[0] 存数前当数组的长度
    
    //调整堆
    void heap(int i) {
    	int left = i * 2;
    	int right = i * 2 + 1;
    	int mins = i;
    	if (left <= arr[0] && arr[left] < arr[mins])
    		mins = left;
    	if (right <= arr[0] && arr[right] < arr[mins])
    		mins = right;
    	if (i != mins) {
    		swap(arr[i], arr[mins]);
    		heap(mins);
    	}
    }
    
    //想堆中插入一个数
    void inheap(int64 key) {
    	arr[++arr[0]] = key;
    	int64 i = arr[0];
    	//i就是前当插入的最后位置. 环循 lgn 次便可
    	while (i > 1 && arr[i] < arr[i / 2]) {
    		swap(arr[i], arr[i / 2]);
    		i = i / 2;
    	}
    }
    
    //回返最小的两个数的和
    int64 get() {
    	int64 p = arr[1], q;
    	arr[1] = arr[arr[0]]; //第一个位置 和 最后一个位置交换
    	arr[0]--; //数组长度减1
    	heap(1); //调整位置1
    	q = arr[1];
    	arr[1] = arr[arr[0]];
    	arr[0]--;
    	heap(1);
    	inheap(p + q); //回返最小的两个数的和
    	return p + q;
    }
    
    int main() {
    	while (cin >> len) {
    		arr[0] = 0;
    		for (int i = 1; i <= len; i++) {
    			cin >> arr[i];
    			inheap(arr[i]);
    		}
    		int64 ans = 0;
    		for (int i = 1; i < len; i++) {
    			ans += get();
    		}
    		cout << ans << endl;
    	}
    	return 0;
    }

        运行时间70ms

        

    文章结束给大家分享下程序员的一些笑话语录: 一程序员告老还乡,想安度晚年,于是决定在书法上有所造诣。省略数字……,准备好文房4宝,挥起毛笔在白纸上郑重的写下:Hello World

  • 相关阅读:
    HDU 3123-GCC(递推)
    新交互英语外挂全自己主动版
    BZOJ 2716 Violet 3 天使玩偶 CDQ分治
    关于 FPGA 和 外部芯片接口时序设计
    Ubuntu启动、停止、重新启动MySQL,查看MySQL错误日志、中文编码错误
    Drupal 7 建站学习手记(四):怎样改动Nivo Slider模块的宽高
    Linux下安装Oracle的过程和涉及的知识点-系列4
    游戏开场镜头拉近(Unity3D开发之四)
    并发编程
    给线程发送消息让它执行不同的处理
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3043334.html
Copyright © 2011-2022 走看看