-
Notifications
You must be signed in to change notification settings - Fork 0
/
HEAP1.cpp
70 lines (64 loc) · 1.62 KB
/
HEAP1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// PREPARING ACM.ICPC 2018
// HEAP1 - CODED BY ICNHOUKDSIIH
// CONTACT ME: https://icnhoukdsiih.blogspot.com/
//--------------------------------------------------
#include <stdio.h>
#define maxn 20000 //So thanh go toi da
typedef long long ll;
ll t,n, heap[maxn+1], nheap,res; //heap dung de luu do dai cac thanh go voi do uu tien cua cac thanh nho hon la lon hon
//Day mot thanh go vao heap
void push(ll value)
{
ll child=++nheap,father;
while (child>=1)
{
father=child/2;
if (father==0 || heap[father]<=value) break;
heap[child]=heap[father];
child=father;
}
heap[child]=value;
}
//Lay thanh go ngan nhat ra khoi heap
ll pop()
{
ll result=heap[1], value=heap[nheap--];
ll father=1,child;
while (father<=nheap)
{
child=father*2;
if (child<nheap && heap[child]>heap[child+1]) child++;
if (child>nheap || heap[child]>=value) break;
heap[father]=heap[child];
father=child;
}
heap[father]=value;
return result;
}
int main()
{
//Doc du lieu dau vao va xu ly
scanf("%lld",&t);
for (ll c=0;c<t;c++)
{
nheap=0;
res=0;
scanf("%lld",&n);
//Doc va day tung thanh go vao heap
for (ll i=0;i<n;i++)
{
ll value;
scanf("%lld",&value);
push(value);
}
//Moi buoc lay hai thanh ngan nhat va cap nhat ket qua, ghep hai thanh lai va day vao heap
for (ll i=0;i<n-1;i++)
{
ll value=pop()+pop();
res+=value;
push(value);
}
printf("%lld\n",res);
}
return 0;
}