-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Add] ED - Tabela Hashing | [Add] AA - Problema dos Retangulos
- Loading branch information
Showing
11 changed files
with
199 additions
and
35 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
500 60 | ||
0 91 75 0 97 0 97 82 18 75 14 0 19 13 98 98 95 9 12 19 29 46 62 6 0 47 60 3 55 49 12 81 4 27 29 71 23 22 50 78 0 56 78 63 14 6 55 62 89 29 18 77 23 0 47 0 40 73 52 0 55 96 19 27 13 0 2 86 35 98 55 9 17 38 0 95 55 72 0 84 0 0 64 93 15 79 7 14 93 45 82 0 0 0 83 67 0 47 29 13 52 2 23 46 81 88 27 82 0 66 0 15 18 79 84 0 13 22 41 11 0 88 89 21 0 13 12 91 2 75 39 17 56 0 0 64 0 49 24 79 79 25 0 59 29 0 88 26 75 74 98 76 22 33 94 60 8 60 0 57 57 41 0 58 0 0 12 77 44 70 7 79 96 33 76 70 28 46 51 38 0 0 6 95 56 0 34 0 67 96 36 37 0 87 51 20 24 11 0 82 42 38 55 46 77 15 57 74 90 50 1 18 0 0 30 56 68 55 0 89 2 93 81 32 54 2 29 84 96 30 83 0 13 0 11 2 23 0 20 80 0 65 0 7 70 41 39 94 43 0 0 56 84 60 94 83 39 8 37 0 86 18 68 32 0 26 75 44 86 0 46 8 0 51 90 88 44 0 92 61 0 66 0 65 0 80 84 45 24 48 64 17 1 29 56 19 0 81 0 0 55 89 72 2 1 0 69 0 70 7 88 11 38 62 73 63 91 0 96 68 47 27 53 80 39 50 38 14 59 0 2 95 35 0 0 54 26 5 93 7 88 70 0 2 38 16 49 4 9 84 24 54 15 35 21 40 0 79 47 0 79 0 1 28 57 62 65 63 99 13 0 46 46 36 56 78 0 15 50 80 95 19 55 60 26 13 86 51 3 33 93 97 30 76 36 93 90 86 85 26 33 0 93 0 0 0 19 75 85 2 0 60 48 90 0 0 29 42 84 19 48 0 91 0 26 39 8 92 81 45 38 73 0 0 19 50 11 5 49 0 48 56 37 45 83 39 10 64 92 21 30 59 36 90 56 93 89 52 0 7 19 0 7 96 93 33 47 43 0 67 18 58 0 33 58 68 16 64 84 81 19 87 52 0 59 0 79 76 17 68 12 0 79 63 16 1 7 0 27 0 |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main() { | ||
int n, k, i, sum = 0, count = 0; | ||
|
||
// Lendo N e K | ||
printf("N e K: "); | ||
scanf("%d %d", &n, &k); | ||
|
||
// Alocando memória para o vetor | ||
int *vet = (int *)malloc(n * sizeof(int)); | ||
|
||
// Lendo o vetor | ||
printf("Vetor: "); | ||
for (i = 0; i < n; i++) { | ||
scanf("%d", &vet[i]); | ||
} | ||
|
||
// Implementando a lógica do dicionário usando um array (considerando valores | ||
// pequenos) | ||
int maxSum = k + 1000000; // Ajustar se necessário | ||
int *dic = (int *)calloc(maxSum, sizeof(int)); | ||
dic[0] = 1; | ||
|
||
for (i = 0; i < n; i++) { | ||
sum += vet[i]; | ||
|
||
// Verificando se (sum - k) existe no "dicionário" | ||
if (sum - k >= 0 && dic[sum - k] > 0) { | ||
count += dic[sum - k]; | ||
} | ||
|
||
// Incrementando a contagem no "dicionário" | ||
if (sum < maxSum) { | ||
dic[sum]++; | ||
} | ||
} | ||
|
||
printf("%d\n", count); | ||
|
||
free(vet); | ||
free(dic); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,26 @@ | ||
#include <stdio.h> | ||
|
||
// 1 <= N <= 500 000 -> Tamanho do vetor | ||
// 0 <= K <= 10^6 -> Soma dos números | ||
// 0 <= Xi <= 100 -> Números dentro do vetor | ||
|
||
void retangulo(int *arr, int n, int k) { | ||
int i, j, soma, solucao = 0; | ||
for (i = 0; i < n - 1; i++) { | ||
soma = 0; | ||
for (j = i; j < n - 1; j++) { | ||
soma += arr[j]; | ||
if (soma > k) { | ||
break; | ||
} | ||
if (soma == k) | ||
solucao++; | ||
#include <stdlib.h> | ||
|
||
int V[500000]; | ||
|
||
int main() | ||
{ | ||
int N, K; | ||
long long saida = 0LL; | ||
long soma; | ||
|
||
scanf("%d %d ", &N, &K); | ||
for (int i = 0 ; i < N ; i++) | ||
scanf("%d ", &V[i]); | ||
|
||
for (int i = 0 ; i < N ; i++){ | ||
soma = 0L; | ||
for (int j = i ; j < N ; j++){ | ||
soma += V[j]; | ||
if (soma == K) | ||
saida++; | ||
} | ||
} | ||
} | ||
|
||
printf("%d", solucao); | ||
} | ||
|
||
int main() { | ||
int k, n; | ||
scanf("%d", &n); | ||
scanf("%d", &k); | ||
|
||
int arr[n]; | ||
|
||
for (int i; i < n; i++) { | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
retangulo(arr, n, k); | ||
|
||
return 0; | ||
printf("%lld\n", saida); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def ret(vet, k: int) -> int: | ||
dic, sum, count = {0: 1}, 0, 0 | ||
|
||
for num in vet: | ||
sum += int(num) | ||
|
||
if (sum - k) in dic: | ||
count += dic[sum - k] | ||
|
||
if sum in dic: | ||
dic[sum] += 1 | ||
else: | ||
dic[sum] = 1 | ||
|
||
return count | ||
|
||
|
||
n, k = input("N e K: ").split(" ") | ||
vet = input("Vetor: ").split(" ") | ||
|
||
print(ret(vet, int(k))) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# O método da Secante | ||
|
||
- O método de Newton-Raphson tem o incoveniente de necessitar da derivada da função | ||
- O método da secante é obtido do método de Newton utilizando a seguinte aproximação para a derivada: | ||
- `f(xR) ≃ f(xR) - f(xR-1) / xR - xR-1` | ||
|
||
``` | ||
No método de Newton | ||
xR+1 = (xR - f(xR)) / f'(xR) | ||
xR+1 = (xR - f(xR)) / (f(xR) - f(xR-1) / xR - xR-1) | ||
xR+1 = xR - f(xR) * (xR - xR-1) / (f(xR) - f(xR-1)) | ||
xR+1 = (xR(f(xR) - f(xR-1)) - f(xR)(xR - xR-1)) / (f(xR) - f(xR-1)) | ||
xR+1 = (xRf(xR) - xRf(xR-1) - xRf(xR) + xR-1f(xR)) / (f(xR) - f(xR-1)) | ||
xR+1 = (xR-1f(xR) - xRf(xR-1)) / (f(xR) - f(xR-1)) | ||
Resulta que | ||
Xn+1 = (Xn-1f(Xn) - Xnf(Xn-1)) / (f(Xn) - f(Xn-1)), | ||
n >= 1 este é o método da secante | ||
``` | ||
|
||
 | ||
|
||
``` | ||
A inclinação da reta é f(x1)-f(x0) / x1 - x0 | ||
e sua equação é | ||
y - f(x0) = ((f(x1)-f(x0)) / x1 - x0) * (x2-x0) | ||
x2 = (x0f(x1) - x1f(x0)) / (f(x1) - f(x0)) | ||
``` | ||
|
||
- A intepretação geométrica é que a reta secante ao gráfico de `f` que passa pelos pontos (Xn-1,f(Xn-1)) e (Xn, f(Xn)) cruza o eixo Ox exatamente no ponto Xn+1 dado por `Xn+1 = (Xn-1f(Xn) - Xnf(Xn-1)) / (f(Xn) - f(Xn-1))` | ||
- **Exemplo:** Considerando a equação x*tan(x) - 1 = 0 utilizando o método da secante para obter uma aproximação de solução positiva. | ||
|
||
> Chame `f(x) = x*tan(x)-1` e tome x0 = 0.75 e x1 = 1 | ||
K | Xk-1 | Xk | f(Xk-1) | f(Xk) | ||
:-- | :-: | :-: | :-: | --: | ||
1 | 0.75 | 1 | -0.301303 | 0.557408 | ||
2 | 1 | 0.837719 | 0.557408 | -0.069692 | ||
3 | 0.837719 | | -0.069692 | |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Calculo Numerico | ||
|
||
- [Aula 01 - Métodos de Funções | Bisseção](./July_15/) | ||
- [Aula 02 - Método da Secante](./August_5/) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Tabela Hash | ||
|
||
## Introdução | ||
|
||
- A maioria das aplicações em computação são compostas por operações do tipo: | ||
- Inserção | ||
- Remoção | ||
- Consulta de dados | ||
|
||
- Essas operações costumam ser na Ordem O(n) | ||
|
||
- Uma tabela hashing é uma generalização de um vetor com `m` posições. | ||
- Cada posição da tabela representa um endereço | ||
- Os elementos a serem armazenados nela possuem um valor-chave que é utilizado para calcular o endereço na tabela onde serão alocados. | ||
|
||
- Uma tabela de espalhamento de tamanho `m` | ||
- Com endereço de 0 a `m-1` | ||
- Dado um conjunto de `C` chaves | ||
- Dado elementos `x` pertencendo a `C` | ||
|
||
- Existe uma função de espalhamento que calcula o endereço onde `x` será armazenado | ||
- A função de espalhamento reduz o espaço de endereços para armazenar os elementos de `C` | ||
|
||
- A maioria das funções de hashing assume que os elementos-chave são números naturais | ||
- Em resumo tabelas hashing são dicionários. | ||
|
||
## Vantagens | ||
|
||
- A principal vantagem está na busca pelos elementos na tabela. | ||
- As operações podem chegar a ordem de O(1) mesmo no pior caso | ||
|
||
## Uma boa função de hashing | ||
|
||
- Deve gerar um número pequeno de colisões | ||
- Ser facilmente computável | ||
|
||
## Função hashing de divisão | ||
|
||
- Uma chave `x` é mapeada em um dos `m` endereços calculando o resto da divisão de `x` por `m` | ||
- `H(x) = x mod m` | ||
|
||
- Exemplo | ||
- `m = 8, x = 100` | ||
- `100 mod 8 = 4` | ||
|
||
## Tratando colisões | ||
|
||
- Endereçamento aberto | ||
- Todas as chaves são armazenadas na própria tabela | ||
- Para números de chaves pequeno | ||
- Posições vazias da tabela são utilizadas para o tratamento de colisões | ||
- Como encontrar as posições vazias? | ||
- Tentativa linear | ||
- Tentativa quadrática | ||
|
||
|