Cattle off the holiday season Team 2 C repair fences (Huffman tree, greedy)

Links: https://ac.nowcoder.com/acm/contest/924/C
Source: Cattle-off network

Fence repair
time limits: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 32768K, other languages 65536k
64bit IO the Format:% LLD
subject description
farmer John hope to repair a small fence around the farm. He measured and found need N (1 <= N <= 20,000) piece of wood, each has a certain integer length Li (1 <= Li <= 50,000) per unit length. He bought a long long wood, just to saw that he needed N piece of wood. (I.e., its length is exactly equal to the sum of Li) FJ ignored kerf, loss of length sawing sawdust produced negligible, you can ignore it.
FJ Unfortunately, he did not find a saw for cutting wood, so he took a long wooden Flanagan came to the farmer Don farm, to ask him to borrow a saw.
Farmers Don is a conservative capitalist, he did not want to borrow a saw FJ, but is willing to cut his own knife which N-1, each time charged FJ. Every time charges exactly equal to the total length of the Okanagan you saw wood. For example, you saw a length of wood 21, it cost 21 cents.
Farmers Don John and then let the farmers decide the order and position of each sawing wood. John saw to help farmers determine the minimum total cost of the N piece of wood. FJ know can have a variety of different cutting methods, different ways may get different total cost, which is in the process because of varying lengths of wood sawing in.
Input Description:
Line. 1: an integer N, the number to be sawed wood
Lines 2..N + 1: a line for each integer representing the length of each wood.
Output Description:
1 Line: An integer that he requires a minimum number of cents, saw N-1 under, saw all the needs of the wood.
Example 1
Input
Copy
. 3
8
5
8
output
copy
34
instructions
he needs from the total length of the wood 21 in three lengths sawing wood 8, 5 and 8.
Wood original length of 8 + 5 + 8 = 21. The first is the cost of the saw 21, it should be cut into two lengths 13 and 8, respectively. The second cost is 13, the length of sawn wood 13 8 and 5. The total cost is 21 + 13 = 34. However, if the first 16 and 21 sawn 5, it will take 16 second, resulting in a total cost of 37 (greater than 34).

Meaning of the questions: thinking: is a classic problem is rather bare and the Huffman tree, we know that every cut about wood, will produce two new pieces of wood, then eventually the whole process would constitute a binary tree, and want the lowest cost, so is the classic problem of optimal binary tree, Huffman tree (Huffman tree), also known as optimal tree, means that a given n-th weights of a leaf node n, a binary tree structure, if the minimum weighted path length , saying such a binary tree is optimal binary tree, also known as Huffman (Huffman tree). Huffman tree is the shortest path length weighted tree, the larger the weight from the root node closer. If the tree node has a certain meaning assigned value, this value is referred to as the right node.



So this question we reverse configuration, you can open a small stack root, each time removing the current minimum length of two pieces of wood, will merge two pieces of wood (wood cut reverse process), the cost is the combined length of the block, has been in this way, the number of pieces of wood until the end of time 1, with a variable value longlong to record the answers.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
priority_queue<int,vector<int>,greater<int> > heap;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    
    gg(n);
    int x;
    repd(i,1,n)
    {
        gg(x);
        heap.push(x);
    }
    ll ans=0ll;
    int y;
    while(heap.size()>1)
    {
        x=heap.top();
        heap.pop();
        y=heap.top();
        heap.pop();
        ans+=1ll*(x+y);
        heap.push(x+y);
    }
    printf("%lld\n",ans );
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

Guess you like

Origin www.cnblogs.com/qieqiemin/p/11022488.html
Recommended