• POJ 3253 Fence Repair (优先队列)


    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into theN 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 theN-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 theN 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 makeN-1 cuts

    Sample Input

    3
    8
    5
    8

    Sample Output

    34

    Hint

    He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
    The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

    题意:有一个农夫要把一个木板钜成几块给定长度的小木板,每一次费用就是当前锯的这个木板的长度  给定小木板的个数n,各个要求的小木板的长度,,求最小费用

    最开始的时候自己按自己的写法写的时候,发现超时了,后来看学长给的题解才知道选用优先队列,可以不知道那是什么鬼,后来查了下;

    比如数据,优先队列为 8 8 5

    取前两个相加,和入队,队列为 13 8 sum=13;第二次 sum=13+13+8=34;

    代码为:

    #include<iostream>
    #include<cstdio>
    typedef long long ll;
    #include<vector>
    #include<queue>
    using namespace std;
    int main()
    {
        int n;  //需要切割的木板个数
        while(cin>>n)
        {
            priority_queue<ll,vector<ll>,greater<int> >Queue;  //定义优先队列,从大到小(队头小,队尾大)
            while(n--)
            {
                ll t;
                cin>>t;
                Queue.push(t);       //输入要求的木板长度(费用)并入队
            }
            ll min=0;   //最小费用
            while(Queue.size()>1)  //当队列中小于等于一个元素时跳出
            {
                ll a=Queue.top(); //得到队首元素的值
                cout<<a<<endl;
                Queue.pop();//出队
                ll b=Queue.top();  //两次取队首,即得到最小的两个值
                cout<<b<<endl;
                Queue.pop();
                Queue.push(a+b);  //入队
                min=min+a+b;
            }
            cout<<min<<endl;
            while(!Queue.empty())  //清空队列
                Queue.pop();
        }
        return 0;
    }



  • 相关阅读:
    阿里巴巴java开发手册学习
    策略模式
    windows常用技巧
    nginx学习
    Tomcat入门
    ThreadPoolTaskExecutor的简单使用
    linux服务器测试性能
    HAProxy1.5.x tcp example
    Haproxy开启日志
    Fix rpmdb: Thread died in Berkeley DB library
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/5679682.html
Copyright © 2020-2023  润新知