poj3253

/** \brief poj 3253
 *
 * \param date 2014/8/8
 * \param state AC
 * \return memory 1124K time 125ms
 *
 */

#include <iostream>
#include <fstream>
#include <queue>
#include <functional>

using namespace std;

struct number
{
    //int x;
    __int64 x;
    bool operator < (const number& a) const{
         return x>a.x;//最小值优先
    }

};

priority_queue<number>que;

//int total,mincost;
__int64 total,mincost;

int main()
{
    //cout << "Hello world!" << endl;
    //freopen("input.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        number num;
        for(int i=0;i<n;i++)
        {
            //int v;
            __int64 v;
            cin>>v;
            num.x=v;
            que.push(num);
        }
        mincost=0;
        total=0;
        while(que.size()>1)
        {
            number v1,v2,temp;
            v1=que.top();
            que.pop();

            v2=que.top();
            que.pop();

            total=(v1.x+v2.x);

            temp.x=total;

            que.push(temp);
            mincost+=total;
        }
        cout<<mincost<<endl;

        while(!que.empty())
            que.pop();
    }
    return 0;
}

poj3253,布布扣,bubuko.com

时间: 2024-12-20 01:00:33

poj3253的相关文章

poj3253 Fence Repair STL优先队列

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=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

poj3253哈夫曼树

Fence Repair Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Farmer John wants to repair a small length of the fence around the pasture. He measures the

POJ3253 Fence Repair 小顶堆+贪心

给了你N个木棒,求把他们组装成一根需要的最小花费,每次只能选两根组装在一起,需要的花费为两个木棒之和, 以前遇到过把一整根切开的,那个是DP,这个则有些类似,可是大胆的猜测了一下,直接每次选取所有木棒中最短的两根,这样就可以了,那么贪心是适用的,但是数量很多,而且两根最短的组装好了得插回去,这样不可能每次都排序吧, 这题首先优先队列肯定是可以做的, 最小堆也是可以的,每次都选出堆里的最小的两个求和再放回去即可 队列本身也就是堆吧,所以差别不大,但是没用过最小堆最大堆的 所以用一次把 #inclu

POJ3253 Haffman

POJ3253 分析: 简单的哈弗曼树的应用. AC代码: 1 //Memory: 316K Time: 16MS 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <queue> 6 7 using namespace std; 8 9 int n; 10 int l; 11 priority_queue <int, vector<int>,

POJ-3253 Fence Repair---Huffman贪心

题目链接: https://vjudge.net/problem/POJ-3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个要求的小木板的长度,及小木板的个数n,求最小费用 思路: HUffman算法 优先队列 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring>

[POJ3253] Fence Repair

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 s

优先队列啦-POJ3253

ence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28008   Accepted: 9098 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) p

哈夫曼树---POJ3253

http://poj.org/problem?id=3253 这就是 最典型的哈夫曼树的题型,我们就根据这道题学习一下哈夫曼树 这是最开始我们把21据下来之后我们据下8,然后再据下5得到34,可以看出13被用到2次,8被用到1次13*2+8=34. 这幅图片,我们先据下21,再据下5,再据下8得到34,即16*2+5=37. 所以,对于这道题,我们运用贪心的思想和哈夫曼树 1 #include<queue> 2 #include<stdio.h> 3 using namespace

POJ3253 Fence Repair(贪心)

切割木板的顺序是自由的,所以每次选择两块最短的板,组合在一起,加入队列,原来两个板出队,直到队列中为空或者只剩下一个板时结束.这里使用优先队列较为方便. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define ll __int64 using namespace std; int len[20005]; in