问题 E: 合并果子-NOIP2004TGT2
时间限制: 1 Sec 内存限制: 128 MB
献花: 20 解决: 17
[献花][花圈][TK题库]
题目描述
合并果子
(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.堆的应用
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
const int MaxN = 10010;
void AdjustDown(int heap[], int high, int idx)
{
int i = idx, j = 2 * i;
while (j <= high)
{
if (j + 1 <= high && heap[j] > heap[j + 1])
++j;
if (heap[i] > heap[j])
swap(heap[i], heap[j]);
else
break;
i = j;
j = 2 * i;
}
}
void InitHeap(int heap[],int n)
{
for (int i = 1; i <= n; ++i)
{
cin >> heap[i];
}
for (int i = n / 2; i >= 1; --i)
{
AdjustDown(heap,n, i);
}
}
int GetTop(int heap[])
{
return heap[1];
}
void DeleteTop(int heap[],int n)
{
swap(heap[1], heap[n]);
AdjustDown(heap, n - 1, 1);
}
void AdjustUp(int heap[], int idx)
{
int i = idx, j = i / 2;
while (j >= 1)
{
if (heap[i] < heap[j])
swap(heap[i], heap[j]);
else
break;
i = j;
j = i / 2;
}
}
void InsertElem(int heap[], int n,int val)
{
heap[n] = val;
AdjustUp(heap, n);
}
int main()
{
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif
std::ios::sync_with_stdio(false);
int n, heap[MaxN],wight,size;
while (cin >> n)
{
InitHeap(heap, n);
if (1 == n)
{
cout << GetTop(heap) << endl;
continue;
}
size = n;
wight = 0;
while (size > 1)
{
int a = GetTop(heap); DeleteTop(heap, size--);
int b = GetTop(heap); DeleteTop(heap, size--);
InsertElem(heap, ++size, a + b);
wight += a + b;
}
cout << wight << endl;
}
return 0;
}
/**************************************************************
Problem: 5068
User: Sharwen
Language: C++
Result: 升仙
Time:65 ms
Memory:1696 kb
****************************************************************/
2.单调队列的应用
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
const int MaxN = 30000;
int main()
{
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif
std::ios::sync_with_stdio(false);
int n, wight;
priority_queue<int, vector<int>, greater<int> > que;
while (cin >> n)
{
while (que.size())
que.pop();
for (int i = 0; i < n; ++i)
{
int w;
cin >> w;
que.push(w);
}
if (que.size() == 1)
{
cout << que.top();
que.pop();
continue;
}
wight = 0;
while (que.size() > 1)
{
int a = que.top();
que.pop();
int b = que.top();
que.pop();
que.push(a + b);
wight += (a + b);
}
cout << wight << endl;
}
return 0;
}
/**************************************************************
Problem: 5068
User: Sharwen
Language: C++
Result: 升仙
Time:155 ms
Memory:1844 kb
****************************************************************/