#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define MAX 20010
using namespace std;
long long Q[MAX];
long long Index;
long long Cost;
int cmp(const void *a,const void *b)
{
return *(int *)b - *(int *)a;
}
void Insert(long long temp)
{
if(Index < 2) return;
for(int i = Index - 2;i >= 0;i--){
if(temp > Q[i])
Q[i+1] = Q[i];
else
{
Q[i+1] = temp;
return;
}
}
Q[0] = temp;
}
int main()
{
int n;
cin>>n;
Cost = 0;
Index = n;
for(int i = 0;i < n;i++)
cin>>Q[i];
qsort(Q,Index,sizeof(long long),cmp);
while(Index > 1){
long long temp = Q[Index-1] + Q[Index-2];
Cost += temp;
Index--;
Insert(temp);
}
cout<<Cost<<endl;
return 0;
}
本文介绍了一个关于计算最优合并成本的问题,通过预读取一定数量的数据并进行排序后,使用特定的插入排序算法来不断合并最小的两个元素以达到最低总成本。此方法适用于需要解决最优成本合并场景的问题。
1352

被折叠的 条评论
为什么被折叠?



