Created
June 26, 2019 13:19
-
-
Save Thiago4532/cda4a231e742169be857bc18cf0f5cc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (n) é o tamanho do vetor | |
// (log_n) é o log na base 2 de n | |
// (v) é o vetor | |
void computa() { | |
// Caso inicial (tamanho 1, j = 0) | |
for (int i = 1; i <= n; i++) | |
tab[i][0] = v[i]; // Intervalo [i, i] | |
for (int j = 1; j <= log_n; j++) { | |
// Note que o for do j vem primeiro, isso é necessário pois | |
// o jeito que tab(i, j) é calculado necessita que todos os i's | |
// para j-1 estejam calculados. | |
for (int i = 1; i <= n; i++) { | |
// Checando se o intervalo que começa em i e tem tamanho 2^j existe. | |
// Para isso basta checar se o fim do intervalo é menor ou igual a n | |
if (i + (1<<j) - 1 > n) break; | |
tab[i][j] = min(tab[i][j-1], tab[i + (1<<(j-1))][j-1]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment