POJ3253
来源:互联网 发布:sql insert case when 编辑:程序博客网 时间:2024/06/05 20:21
#include <iostream>
#include <stdio.h>
#include <string.h>
#define maxn 20000
using namespace std;
int l[maxn];
int n,h;
long long int minx;
long long int ans;
void heap_sort( int x)//小根堆
{
int lg, lr, t;
while( (x<<1) <= h)
{
lg = x << 1;
lr = ( x << 1) + 1;
if( lr <= h && l[lg] > l[lr])
lg = lr;
if( l[x] > l[lg])
{
t = l[x];
l[x] = l[lg];
l[lg] = t;
x = lg;
}
else
break;
}
return ;
}
int main()
{
int i;
while( 1 == scanf("%d",&n))
{
for( i = 1; i<=n; i++)
scanf("%d",&l[i]);
h = n;
for( i = n/2; i > 0 ; i--)//堆排序
heap_sort(i);
ans = 0;
while( h > 1)//去顶端堆
{
minx = l[1];
l[1] = l[h];
h--;
heap_sort(1);
minx += l[1];
l[1] = minx;
heap_sort(1);
ans += minx;
}
printf("%I64d\n",ans);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#define maxn 20000
using namespace std;
int l[maxn];
int n,h;
long long int minx;
long long int ans;
void heap_sort( int x)//小根堆
{
int lg, lr, t;
while( (x<<1) <= h)
{
lg = x << 1;
lr = ( x << 1) + 1;
if( lr <= h && l[lg] > l[lr])
lg = lr;
if( l[x] > l[lg])
{
t = l[x];
l[x] = l[lg];
l[lg] = t;
x = lg;
}
else
break;
}
return ;
}
int main()
{
int i;
while( 1 == scanf("%d",&n))
{
for( i = 1; i<=n; i++)
scanf("%d",&l[i]);
h = n;
for( i = n/2; i > 0 ; i--)//堆排序
heap_sort(i);
ans = 0;
while( h > 1)//去顶端堆
{
minx = l[1];
l[1] = l[h];
h--;
heap_sort(1);
minx += l[1];
l[1] = minx;
heap_sort(1);
ans += minx;
}
printf("%I64d\n",ans);
}
return 0;
}
0 0
- poj3253
- POJ3253
- poj3253
- poj3253
- poj3253
- poj3253
- POJ3253
- poj3253
- POJ3253
- poj3253
- poj3253
- POJ3253
- poj3253
- poj3253
- poj3253
- poj3253
- poj3253
- poj3253
- Java异常机制,你知道吗?
- 淘宝商城servlet部分
- cocos2d-x实现多个精灵动画同步播放(一)
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问
- Android扫描多媒体文件剖析
- POJ3253
- Trie树
- 姐们儿 想减肥深夜别吃这些东西
- Java常用工具类四
- android 布局权重问题(最近布局经常坑爹)
- 淘宝商城servlet的路径web.xml部分
- php控制结构语句declare中的tick的详解[整理版]
- 字典树
- SQLServer如何导入excel