zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 31- D. Boxes And Balls

    D. Boxes And Balls

    time limit per test2 seconds
    memory limit per test256 megabytes

    题目链接:http://codeforces.com/contest/884/problem/D

    Description

    Ivan has n different boxes. The first of them contains some balls of n different colors.

    Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 ≤ i ≤ n) i-th box will contain all balls with color i.

    In order to do this, Ivan will make some turns. Each turn he does the following:

    Ivan chooses any non-empty box and takes all balls from this box;
    Then Ivan chooses any k empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3.
    The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.

    Help Ivan to determine the minimum possible penalty of the game!

    Input

    The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of boxes and colors.

    The second line contains n integer numbers a1, a2, …, an (1 ≤ ai ≤ 109), where ai is the number of balls with color i.

    Output

    Print one number — the minimum possible penalty of the game.
    这里写图片描述
    Note
    In the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes. Penalty is 6.

    In the second example you make two turns:

    1. Take all the balls from the first box, choose k = 3, put balls of color 3 to the third box, of color 4 — to the fourth box and the rest put back into the first box. Penalty is 14;
    2. Take all the balls from the first box, choose k = 2, put balls of color 1 to the first box, of color 2 — to the second box. Penalty is 5.
      Total penalty is 19.

    题目精简之后可以看成一个sum分解成一个数列,每次分解的代价就是当前的sum,其实仔细想想就是一个3叉哈夫曼树,当不能凑成三叉的哈夫曼树的时候可以添加0来凑数(不会改变sum)。


    哈夫曼树详解:https://www.cnblogs.com/mcgrady/p/3329825.html


    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    priority_queue <ll,vector<ll>,greater<ll> > qu;
    int main()
    {
        int n;
        scanf("%d",&n);
        while(n--)
        {
            ll now;
            scanf("%lld",&now);
            qu.push(now);
        }
        if(qu.size()%2 == 0)
            qu.push(0);
        ll ans = 0,a,b,c;
        while(qu.size() > 1)
        {
            a = qu.top(); qu.pop();
            b = qu.top(); qu.pop();
            c = qu.top(); qu.pop();
            ans += a + b + c;
            qu.push(a+b+c);
        }
        printf("%lld",ans);
        return 0;
    }
    
  • 相关阅读:
    求n(n>=2)以内的质数/判断一个数是否质数——方法+细节优化
    poj1185炮兵阵地 正确代码及错误代码分析
    运算符优先级的几点注意
    mod(%)之规律(除数与被除数的正负分析)
    css背景
    Content-Type
    vue数组的增改和v-model的绑定使用Demo
    python open函数关于w+ r+ 读写操作的理解(转)
    http状态码解释
    cookie与token对比(转)
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107252.html
Copyright © 2011-2022 走看看