poj3253
来源:互联网 发布:谁杀了宇智波一族 知乎 编辑:程序博客网 时间:2024/06/06 00:54
本题用的思想是赫夫曼树的思想,当然,没有那个思想那么复杂,我最开始用的是最复杂,然后意料之中的是超时,然后不断的化简,其实是每次找到所有数中最小的两个数,然后将其取出队列然后将和再次放入队列,最后求所有的和的过程,由于没有学过堆排序,我尝试的是使用快排,当然还是超时,然后在网上搜了一下堆排序的东西,然后看了一下别人的代码,然后过了!
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int n;
__int64 a[20005];
int flag=0;
priority_queue<int,vector<int>,greater<int> > q;
cin>>n;
for(int i=0; i<n; i++)
{
scanf("%I64d",&a[i]);
q.push(a[i]);//推入排序队列中
}
__int64 sum1=0,sum2=0;
while(1)
{
if(n==1)//只有一个元素的
{
printf("%I64d\n",a[0]);
flag=1;
break;
}
int m1=q.top();//取最小的元素
q.pop();//进行更新吧
int m2=q.top();
q.pop();
sum1=m1+m2;
sum2+=sum1;//记录的是总的需要的钱
if(q.empty())
break;
q.push(sum1);
}
if(flag==0)
printf("%I64d\n",sum2);
return 0;
}
- poj3253
- POJ3253
- poj3253
- poj3253
- poj3253
- poj3253
- POJ3253
- poj3253
- POJ3253
- poj3253
- poj3253
- POJ3253
- poj3253
- poj3253
- poj3253
- poj3253
- poj3253
- poj3253
- HDU 4462(Scaring the Birds)
- 子对话框显示图标问题
- oracle使用闪回找回数据
- VirtualBox虚拟机网络设置(四种方式)
- JS的try catch finally
- poj3253
- 拷贝image文件到image.xml的命令
- JSP开发技术一
- 指针的引用(*&)与指针的指针(**)
- Windows游戏设计(一)- 拼图游戏 - 使用Win32 SDK
- HDU 4445 Crazy Tank(枚举角度)
- 利用内存linux中加速Firefox和Chrome的载入速度
- 书同文,币同制,车同轨
- 友元的一点笔记