-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacacomg.cpp
68 lines (46 loc) · 1.42 KB
/
macacomg.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
#include <iostream>
void merge_sort(int *array, int tamanho) {
int *auxiliar = new int[tamanho];
int esquerda,meio, direita, x, y;
for (int i = 1; i < tamanho; i*=2) {
for (int j = 0; j + i < tamanho; j+= i*2)
{
esquerda = j;
meio = esquerda + i;
direita = esquerda + 2*i;
if (direita > tamanho) direita = tamanho;
x = esquerda;
y = meio;
while (esquerda < direita)
{
if((array[x] < array[y] || y >= direita) && (x < meio)) auxiliar[esquerda++] = array[x++];
else if((array[y] <= array[x] || x>= meio) && y < direita) auxiliar[esquerda++] = array[y++];
}
for (int k = j; k < direita; k++)
array[k] = auxiliar[k];
}
}
delete[] auxiliar;
}
int main() {
int n;
while (1)
{
std::cin >> n;
if(!n) break;
int *p = new int[n];
for (int cont = 0; cont < n; cont++)
std::cin >> p[cont];
merge_sort(p,n);
int i = 0, j = n-1, k = 0;
int maior;
while (i < j) {
if(k == 0 || p[i] + p[j] > maior) maior = p[i] + p[j];
k++;
i++;
j--;
}
std::cout << maior << std::endl;
delete[] p;
}
}