-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inicialización del repositorio con los notebooks a la fecha actual
- Loading branch information
Gerson Lázaro
committed
Oct 12, 2015
1 parent
3cd5f2b
commit 2012d74
Showing
54 changed files
with
3,188 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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,2 +1,7 @@ | ||
# notebook | ||
Notebook para ICPC utilizado en competencias oficiales | ||
#### Notebook UFPS-ICPC | ||
|
||
Notebooks oficiales de la Universidad Francisco de Paula Santander en java y C++ para competencias ICPC. | ||
|
||
Los archivos actuales compilados se encuentran en latex y pdf, en el folder "Notebooks Compilados", mientras que los folders de cada lenguaje contienen los códigos propios del lenguaje. | ||
|
||
Se invita a los estudiantes del semillero a aportar sus propios códigos al notebook, o bien, a indicar temas que consideran deberian ser incluidos mediante issues. |
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,11 @@ | ||
#include <cstdio> | ||
|
||
scanf("%d",&value); //int | ||
scanf("%ld",&value); //long y long int | ||
scanf("%c",&value); //char | ||
scanf("%f",&value); //float | ||
scanf("%lf",&value); //double | ||
scanf("%s",&value); //char* | ||
scanf("%lld",&value); //long long int | ||
scanf("%x",&value); //int hexadecimal | ||
scanf("%o",&value); //int octal |
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,31 @@ | ||
Dados N articulos, cada uno con su propio valor y peso y un tamaño maximo de una mochila, se debe calcular el valor maximo de los elementos que es posible llevar. | ||
Debe seleccionarse un subconjunto de objetos, de tal manera que quepan en la mochila y representen el mayor valor posible. | ||
|
||
#include <algorithm> | ||
|
||
const int MAX_WEIGHT = 40;//Peso maximo de la mochila | ||
const int MAX_N = 1000; //Numero maximo de objetos | ||
int N;//Numero de objetos | ||
int prices[MAX_N];//precios de cada producto | ||
int weights[MAX_N];//pesos de cada producto | ||
int memo[MAX_N][MAX_WEIGHT];//tabla dp | ||
|
||
//El metodo debe llamarse con 0 en el id, y la capacidad de la mochila en w | ||
int knapsack(int id, int w) { | ||
if (id == N || w == 0) { | ||
return 0; | ||
} | ||
if (memo[id][w] != -1) { | ||
return memo[id][w]; | ||
} | ||
if (weights[id] > w){ | ||
memo[id][w] = knapsack(id + 1, w); | ||
}else{ | ||
memo[id][w] = max(knapsack(id + 1, w), prices[id] + knapsack(id + 1, w - weights[id])); | ||
} | ||
|
||
return memo[id][w]; | ||
} | ||
|
||
//La tabla memo debe iniciar en -1 | ||
memset(memo, -1, sizeof memo); |
21 changes: 21 additions & 0 deletions
21
c++/Dynamic Programming/Longest Increasing Subsequence.cpp
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 @@ | ||
Halla la longitud de la subsecuencia creciente mas larga. MAX debe definirse en el tamaño limite del array, n es el tamaño del array. Puede aplicarse también sobre strings, cambiando el parametro int s[] por string s. Si debe ser estrictamente creciente, cambiar el <= de s[j] <= s[i] por < | ||
|
||
const int MAX = 1005; | ||
int memo[MAX]; | ||
|
||
int longestIncreasingSubsequence(int s[], int n){ | ||
memo[0] = 1; | ||
int output = 0; | ||
for (int i = 1; i < n; i++){ | ||
memo[i] = 1; | ||
for (int j = 0; j < i; j++){ | ||
if (s[j] <= s[i] && memo[i] < memo[j] + 1){ | ||
memo[i] = memo[j] + 1; | ||
} | ||
} | ||
if(memo[i] > output){ | ||
output = memo[i]; | ||
} | ||
} | ||
return output; | ||
} |
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,32 @@ | ||
Algoritmo de búsqueda en anchura en grafos, recibe un nodo inicial s y visita todos los nodos alcanzables desde s. BFS también halla la distancia más corta entre el nodo inicial s y los demás nodos si todas las aristas tienen peso 1. | ||
|
||
int v, e; //vertices, arcos | ||
int MAX=100005; | ||
vector<int> ady[MAX]; //lista de Adyacencia | ||
long long distance[MAX]; | ||
|
||
static void init() { | ||
for (int j = 0; j <= v; j++) { | ||
distance[j] = -1; | ||
ady[j].clear(); | ||
} | ||
} | ||
|
||
static void bfs(int s){ | ||
queue<int> q; | ||
q.push(s); //Inserto el nodo inicial | ||
distance[s]=0; | ||
int actual, i, next; | ||
|
||
while(q.size()>0){ | ||
actual=q.front(); | ||
q.pop(); | ||
for(i=0; i<ady[actual].size(); i++){ | ||
next=ady[actual][i]; | ||
if(distance[next]==-1){ | ||
distance[next]=distance[actual]+1; | ||
q.push(next); | ||
} | ||
} | ||
} | ||
} |
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,25 @@ | ||
Algoritmo de búsqueda en profundidad para grafos. Parte de un nodo inicial s visita a todos sus vecinos. DFS puede ser usado para contar la cantidad de componentes conexas en un grafo y puede ser modificado para que retorne información de los nodos dependiendo del problema. Permite hallar ciclos en un grafo. | ||
|
||
int v, e; //vertices, arcos | ||
int MAX=100005; | ||
vector<int> ady[MAX]; | ||
int marked[MAX]; | ||
|
||
void init(){ | ||
for (int j = 0; j <= v; j++) { | ||
marked[j] = 0; | ||
ady[j].clear(); | ||
} | ||
} | ||
|
||
static void dfs(int s){ | ||
marked[s]=1; | ||
int i, next; | ||
|
||
for(i=0; i<ady[s].size(); i++){ | ||
next=ady[s][i]; | ||
if(marked[next]==0){ | ||
dfs(next); | ||
} | ||
} | ||
} |
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 @@ | ||
Algoritmo que dado un grafo con pesos no negativos halla la ruta mínima entre un nodo inicial s y todos los demás nodos. | ||
|
||
int v,e; | ||
vector<Node> ady[100001]; | ||
int marked[100001]; | ||
long long distance[100001]; | ||
int prev[100001]; | ||
|
||
class cmp | ||
{ | ||
public: | ||
bool operator()(Node n1,Node n2) | ||
{ | ||
if(n1.second>n2.second) | ||
return true; | ||
else | ||
return false; | ||
} | ||
}; | ||
|
||
void init(){ | ||
long long max=LLONG_MAX; | ||
for(int j=0; j<=v; j++){ | ||
ady[j].clear(); | ||
marked[j]=0; | ||
prev[j]=-1; | ||
distance[j]=max; | ||
} | ||
} | ||
|
||
void dijkstra(int s){ | ||
priority_queue< Node , vector<Node> , cmp > pq; | ||
pq.push(Node(s, 0));//se inserta a la cola el nodo Inicial. | ||
distance[s]=0; | ||
int actual, j, adjacent; | ||
long long weight; | ||
|
||
while(!pq.empty()){ | ||
actual=pq.top().first; | ||
pq.pop(); | ||
if(marked[actual]==0){ | ||
marked[actual]=1; | ||
for(j=0; j<ady[actual].size(); j++){ | ||
adjacent=ady[actual][j].first; | ||
weight=ady[actual][j].second; | ||
if(marked[adjacent]==0){ | ||
if(distance[adjacent]>distance[actual]+weight){ | ||
distance[adjacent]=distance[actual]+weight; | ||
prev[adjacent]=actual; | ||
pq.push(Node(adjacent, distance[adjacent])); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,27 @@ | ||
Algoritmo para grafos que halla la distancia mínima entre cualquier par de nodos. Matrix[i][j] guardará la distancia mínima entre el nodo i y el j. | ||
|
||
#define Node pair<int,long long> //(Vertice adyacente, peso) | ||
|
||
int v,e; | ||
int matrix[505][505]; | ||
|
||
void floydWarshall(){ | ||
int k=0; | ||
int aux, i ,j; | ||
|
||
while(k<v){ | ||
for(i=0; i<v; i++){ | ||
if(i!=k){ | ||
for(j=0; j<v; j++){ | ||
if(j!=k){ | ||
aux=matrix[i][k]+matrix[k][j]; | ||
if(aux<matrix[i][j] && aux>0){ | ||
matrix[i][j]=aux; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
k++; | ||
} | ||
} |
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,81 @@ | ||
Algoritmo para hallar el arbol cobertor mínimo de un grafo no dirigido y conexo. Utiliza la técnica de Union-Find(Conjuntos disjuntos) para detectar que aristas generan ciclos. | ||
Para hallar los 2 arboles cobertores minimos, se debe ejecutar el algoritmo v-1 veces, en cada una de ellas dscartar una de las aristas previamente elegidas en el arbol. | ||
|
||
struct Edge{ | ||
int origen, destino, peso; | ||
|
||
bool operator!=(const Edge& rhs) const{ | ||
if(rhs.origen!=origen || rhs.destino!=destino || rhs.peso!=peso){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
int v,e; | ||
int MAX=10001; | ||
int parent[MAX]; | ||
int rank[MAX]; | ||
Edge edges[MAX]; | ||
Edge answer[MAX]; | ||
|
||
void init(){ | ||
for(int i=0; i<v; i++){ | ||
parent[i]=i; | ||
rank[i]=0; | ||
} | ||
} | ||
|
||
int cmp(const void* a, const void* b) | ||
{ | ||
struct Edge* a1 = (struct Edge*)a; | ||
struct Edge* b1 = (struct Edge*)b; | ||
return a1->peso > b1->peso; | ||
} | ||
|
||
int find(int i){ | ||
if(parent[i]!=i){ | ||
parent[i]=find(parent[i]); | ||
} | ||
return parent[i]; | ||
} | ||
|
||
void unionFind(int x, int y){ | ||
int xroot = find(x); | ||
int yroot = find(y); | ||
|
||
// Attach smaller rank tree under root of high rank tree | ||
// (Union by rank) | ||
if (rank[xroot] < rank[yroot]) | ||
parent[xroot] = yroot; | ||
else if (rank[xroot] > rank[yroot]) | ||
parent[yroot] = xroot; | ||
|
||
// If ranks are same, then make one as root and increment | ||
// its rank by one | ||
else{ | ||
parent[yroot] = xroot; | ||
rank[xroot]++; | ||
} | ||
} | ||
|
||
void kruskall(){ | ||
Edge actual; | ||
int aux=0; | ||
int i=0; | ||
int x,y; | ||
qsort(edges, e, sizeof(edges[0]), cmp); | ||
|
||
while(aux<v-1){ | ||
actual=edges[i]; | ||
x=find(actual.origen); | ||
y=find(actual.destino); | ||
|
||
if(x!=y){ | ||
answer[aux]=actual; | ||
aux++; | ||
unionFind(x, y); | ||
} | ||
i++; | ||
} | ||
} |
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,53 @@ | ||
Algoritmo para hallar los puentes e itsmos en un grafo no dirigido. | ||
|
||
vector<int> ady[1010]; | ||
int marked[1010]; | ||
int prev[1010]; | ||
int dfs_low[1010]; | ||
int dfs_num[1010]; | ||
int itsmos[1010]; | ||
int n, e; | ||
int dfsRoot,rootChildren,cont; | ||
vector<pair<int,int>> bridges; | ||
|
||
void init(){ | ||
bridges.clear(); | ||
cont=0; | ||
int i; | ||
for(i=0; i<n; i++){ | ||
ady[i].clear(); | ||
marked[i]=0; | ||
prev[i]=-1; | ||
itsmos[i]=0; | ||
} | ||
} | ||
|
||
void dfs(int u){ | ||
dfs_low[u]=dfs_num[u]=cont; | ||
cont++; | ||
marked[u]=1; //esto no estaba >.< | ||
int j, v; | ||
|
||
for(j=0; j<ady[u].size(); j++){ | ||
v=ady[u][j]; | ||
if(marked[v]==0){ | ||
prev[v]=u; | ||
//para el caso especial | ||
if(u==dfsRoot){ | ||
rootChildren++; | ||
} | ||
dfs(v); | ||
//PARA ITSMOS | ||
if(dfs_low[v]>=dfs_num[u]){ | ||
itsmos[u]=1; | ||
} | ||
//PARA bridges | ||
if(dfs_low[v]>dfs_num[u]){ | ||
bridges.push_back(make_pair(min(u,v),max(u,v))); | ||
} | ||
dfs_low[u]=min(dfs_low[u], dfs_low[v]); | ||
}else if(v!=prev[u]){ //Arco que no sea backtrack | ||
dfs_low[u]=min(dfs_low[u], dfs_num[v]); | ||
} | ||
} | ||
} |
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,15 @@ | ||
Realiza a^b y retorna el resultado módulo c. Si se elimina el módulo c, debe tenerse precaución para no exceder el límite | ||
|
||
|
||
int binaryExponentiation(int a, int b, int c){ | ||
if (b == 0){ | ||
return 1; | ||
} | ||
if (b % 2 == 0){ | ||
int temp = binaryExponentiation(a,b/2, c); | ||
return ((long long)(temp) * temp) % c; | ||
}else{ | ||
int temp = binaryExponentiation(a, b-1, c); | ||
return ((long long)(temp) * a) % c; | ||
} | ||
} |
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,13 @@ | ||
Calcula el coeficiente binomial nCr, entendido como el número de subconjuntos de k elementos escogidos de un conjunto con n elementos. | ||
|
||
long long binomialCoefficient(long long n, long long r) { | ||
if (r < 0 || n < r) { | ||
return 0; | ||
} | ||
r = min(r, n - r); | ||
long long ans = 1; | ||
for (int i = 1; i <= r; i++) { | ||
ans = ans * (n - i + 1) / i; | ||
} | ||
return ans; | ||
} |
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,11 @@ | ||
Guarda en el array Catalan Numbers los numeros de Catalan hasta MAX. | ||
|
||
const int MAX = 30; | ||
long long catalanNumbers[MAX+1]; | ||
|
||
void catalan(){ | ||
catalanNumbers[0] = 1; | ||
for(int i = 1; i <= MAX; i++){ | ||
catalanNumbers[i] = (long long)(catalanNumbers[i-1]*((double)(2*((2 * i)- 1))/(i + 1))); | ||
} | ||
} |
Oops, something went wrong.