问题 E: 合并果子-NOIP2004TGT2

题目描述

合并果子

(fruit.pas/c/cpp)

  在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。

  因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。
  例如有3种果子,数目依次为1,2,9。可以先将1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为12。
所以多多总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。

输入

  输入包括两行,第一行是一个整数n(1<=n<=10000),表示果子的种类数。
  第二行包含n个整数,用空格分隔,第i个整数ai(1<=ai<=20000)是第i种果子的数目。

输出

  输出包括一行,这一行只包含一个整数,也就是最小的体力耗费值。输入数据保证这个值小于2^31。

样例输入

3
1 2 9

样例输出

15

提示


本题请用两张算法完成:


1.堆的应用


2.单调队列的应用

1.优先队列

#include <vector>
#include <queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int n;
int main()
{
    int i, temp, x, y, res;
    while (cin >> n)
    {
        res = 0;
        for (i = 1; i <= n; i++)
        {
            cin >> temp;
            q.push(temp);
        }
        while (q.size() > 1)
        {
            x = q.top();
            q.pop();
            y = q.top();
            q.pop();
            temp = x + y;
            res += temp;
            q.push(temp);
        }
        cout<<res<<endl;
    }
    return 0;
}
/**************************************************************
    Problem: 5068
    User: morizunzhu
    Language: C++
    Result: 正确
    Time:24 ms
    Memory:2164 kb
****************************************************************/

2.维护最小堆

#include <iostream>
#define MAX_SIZE 10002
using namespace std;
int heap[MAX_SIZE], n;
void upAdjust(int low, int high)
{
    int i = high, j = i / 2;
    while (j >= low)
    {
        if (heap[j] > heap[i])
        {
            swap(heap[j], heap[i]);
            i = j;
            j = i / 2;
        }
        else
        {
            break;
        }
    }
}
void downAdjust(int low, int high)
{
    int i = low, j = 2 * i;
    while (j <= high)
    {
        if ((j + 1) <= high && heap[j + 1] < heap[j])
        {
            j = j + 1;
        }
        if (heap[i] > heap[j])
        {
            swap(heap[i], heap[j]);
            i = j;
            j = 2 * i;
        }
        else
        {
            break;
        }
    }
}
int make()
{
    int i, res = 0, x, y, temp;
    for (i = 1; i <= n; i++)
    {
        cin >> heap[i];
        upAdjust(1, i);
    }
    int len = n;
    while (len > 1)
    {
        x = heap[1];
        heap[1] = heap[len];
        --len;
        downAdjust(1, len);
        y = heap[1];
        temp = x + y;
        res += temp;
        heap[1] = temp;
        downAdjust(1, len);
    }
    return res;
}
int main()
{
 
    while (cin >> n)
    {
        cout << make() << endl;
    }
 
    return 0;
}
 
/**************************************************************
    Problem: 5068
    User: morizunzhu
    Language: C++
    Result: 正确
    Time:8 ms
    Memory:2056 kb
****************************************************************/

猜你喜欢

转载自blog.csdn.net/Morizunzhu/article/details/81501231