合并果子
2006-10-26 19:28 老博客哈 阅读(1107) 评论(0) 收藏 举报
/*http://coder.buct.edu.cn:8080/JudgeOnline/showproblem?problem_id=1078*/
好久没有做题目了, 找了一个哈夫曼树的题目。
 #include <iostream>
#include <iostream>
 #include <functional>
#include <functional>
 #include <algorithm>
#include <algorithm>
 #include <vector>
#include <vector>

 using namespace std;
using namespace std;

 int main()
int main()
 {
{
 int n, e;
    int n, e;
 vector<int> s;
    vector<int> s;
 int i;
    int i;
 int total;
    int total;

 while(cin >> n)
    while(cin >> n)
 {
    {
 s.clear();
        s.clear();
 for( i = 0; i < n; i++)
        for( i = 0; i < n; i++)
 {
        {
 cin >> e;
            cin >> e;
 s.push_back(e);
            s.push_back(e);
 }
        }
 //建立堆
        //建立堆
 make_heap(s.begin(), s.end(), greater<int>());
        make_heap(s.begin(), s.end(), greater<int>());
 total = 0;
        total = 0;
 int tmp = 0;
        int tmp = 0;
 for(i = 0; i < n - 1; i++)
        for(i = 0; i < n - 1; i++)
 {
        {
 tmp = s[0];
            tmp = s[0];
 pop_heap(s.begin(), s.end(),greater<int>());
            pop_heap(s.begin(), s.end(),greater<int>());
 s.pop_back();//移除最小元素
            s.pop_back();//移除最小元素
 tmp += s[0];
            tmp += s[0];    
 pop_heap(s.begin(), s.end(), greater<int>());
            pop_heap(s.begin(), s.end(), greater<int>());
 s.pop_back();//移除次小元素
            s.pop_back();//移除次小元素

 total += tmp;
            total += tmp;    
 s.push_back(tmp);
            s.push_back(tmp);
 push_heap(s.begin(), s.end(), greater<int>());
            push_heap(s.begin(), s.end(), greater<int>());
 }
        }
 cout << total << endl;
        cout << total << endl;
 }
    }

 return 0;
    return 0;
 }
}
 
好久没有做题目了, 找了一个哈夫曼树的题目。
 #include <iostream>
#include <iostream> #include <functional>
#include <functional> #include <algorithm>
#include <algorithm> #include <vector>
#include <vector>
 using namespace std;
using namespace std;
 int main()
int main() {
{ int n, e;
    int n, e; vector<int> s;
    vector<int> s; int i;
    int i; int total;
    int total;
 while(cin >> n)
    while(cin >> n) {
    { s.clear();
        s.clear(); for( i = 0; i < n; i++)
        for( i = 0; i < n; i++) {
        { cin >> e;
            cin >> e; s.push_back(e);
            s.push_back(e); }
        } //建立堆
        //建立堆 make_heap(s.begin(), s.end(), greater<int>());
        make_heap(s.begin(), s.end(), greater<int>()); total = 0;
        total = 0; int tmp = 0;
        int tmp = 0; for(i = 0; i < n - 1; i++)
        for(i = 0; i < n - 1; i++) {
        { tmp = s[0];
            tmp = s[0]; pop_heap(s.begin(), s.end(),greater<int>());
            pop_heap(s.begin(), s.end(),greater<int>()); s.pop_back();//移除最小元素
            s.pop_back();//移除最小元素 tmp += s[0];
            tmp += s[0];     pop_heap(s.begin(), s.end(), greater<int>());
            pop_heap(s.begin(), s.end(), greater<int>()); s.pop_back();//移除次小元素
            s.pop_back();//移除次小元素
 total += tmp;
            total += tmp;     s.push_back(tmp);
            s.push_back(tmp); push_heap(s.begin(), s.end(), greater<int>());
            push_heap(s.begin(), s.end(), greater<int>()); }
        } cout << total << endl;
        cout << total << endl; }
    }
 return 0;
    return 0; }
}
 
                    
                     
                    
                 
                    
                

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号