Skip to content

Instantly share code, notes, and snippets.

@Thiago4532
Created June 26, 2019 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thiago4532/cda4a231e742169be857bc18cf0f5cc7 to your computer and use it in GitHub Desktop.
Save Thiago4532/cda4a231e742169be857bc18cf0f5cc7 to your computer and use it in GitHub Desktop.
// (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