K Fork Huffman Tree

Source: Internet
Author: User
Tags first row

There is a huge ant kingdom in the Description Valley, with a neat square outside the nest, where ants often hold various activities in good weather. This night, the day of the dust, the 2nd day, the square is full of large and small heap of fruit, ant whistle out of the number of a total of n heap, the Queen asked her subjects to the square on the fruit heap clean off. This is done by combining any of the K-piles of fruit in the square into a pile and repeating it until all the fruit stacks are eventually merged into a pile. Regulation (1): 2≤k≤m,m is specified by the Queen, (2): Each time the cost of merging K-heap fruit is the weight of this K-heap fruit son and.
Your task is to calculate the minimum total cost of adding n heap of fruit to 1 heaps for a given N and M.
For example, there are 7 piles of fruit on the square, the weight of which is 45,13,12,16,9,5,22. When m=3, the minimum total cost of these fruit heaps to be combined into a heap is 199. When m=5, the minimum total cost of these fruit heaps to be combined into a heap is 148.
Input contains n+2 integers (n≤100000), where the first row of 2 positive integers represents the n heap of fruit and the maximum number of heaps of m that can be combined each time the merge, starting with n numbers from the second row, representing the weight of the N heap fruit (1~500), separated by a space between the number and the number.
Output contains only a positive integer that represents the minimum total cost required to combine the n heap of fruit into 1 heaps.
Sample Input

7 3
45 13 12 16 9 5 22
Sample Output

This is a K-fork Huffman Tree template problem, heap plus some processing can be

Code:

#include <bits/stdc++.h> using namespace std;
int a,b,c,ans,n,m,sum,f[200001];
    void Add (int s) {int now,now;
    F[++sum]=s;
    Now=sum;
        while (now>1) {NOW=NOW/2;
        if (F[now]<f[now]) swap (F[now],f[now]);
        else return;    
    Now=now;
    }} int get () {int now,now,k=f[1];
    f[1]=f[sum--];
    Now=1;
        while (now*2<=sum) {now=now*2;
        if (now<sum&&f[now]>f[now+1]) now++;
        if (F[now]>f[now]) swap (F[now],f[now]);
        else return k;    
    Now=now;
} return k;
    } int main () {int k;
    Ios::sync_with_stdio (FALSE);
    cin>>n>>k; 
    for (int i=1;i<=n;i++) Cin>>m,add (m);
    int x= (K-1)-(n-1)% (k-1);
    if (x==k-1) x=0;
    for (int i=1;i<=x;i++) Add (0);
        while (sum!=1) {int sum=0;
        for (int i=1;i<=k;i++) A=get (), sum+=a;   
        Ans+=sum;
 Add (sum);   } cout<<ans; }

Pile of hand-masturbate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.