-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.c
67 lines (65 loc) · 938 Bytes
/
merge.c
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
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
int main()
{
long n;
scanf("%ld", &n);
long arn[n];
long sum = 0;
for (long i = 0; i<n;i++)
{
scanf("%ld", &arn[i]);
sum += arn[i];
}
long pq[sum];
long cpq = 0;
for (long i = 0; i<sum;i++)
{
long x;
scanf("%ld", &x);
long ii = cpq;
cpq++;
pq[ii] = x;
while ((ii > 0) && (pq[(ii-1)/2] > pq[ii]))
{
long c = pq[(ii-1)/2];
pq[(ii-1)/2] = pq[ii];
pq[ii] = c;
ii = (ii-1)/2;
}
}
for (long i = 0; i<sum;i++)
{
printf("%ld\n", pq[0]);
cpq --;
if (cpq > 0)
{
pq[0] = pq[cpq];
long ii = 0;
while (1)
{
long l = 2*ii+1;
long r = l+1;
long j = ii;
if ((l<cpq) && pq[ii] > pq[l])
{
ii = l;
}
if ((r<cpq) && pq[ii] > pq[r])
{
ii = r;
}
if (ii == j)
{
break;
}
long c = pq[ii];
pq[ii] = pq[j];
pq[j] = c;
}
}
}
return 0;
}