diff --git a/src/cw12/zad1/kinoman.cpp b/src/cw12/zad1/kinoman.cpp new file mode 100644 index 0000000..62ce8a3 --- /dev/null +++ b/src/cw12/zad1/kinoman.cpp @@ -0,0 +1,31 @@ +/* +sortujemy po końcach +przeglądamy od najwcześniejszych +*/ + +#include +#include +#include +#include + +bool compare_second(const std::pair &a, const std::pair &b) { + return a.second < b.second; +} + +int kinoman(std::vector> teletydzien) { + std::sort(teletydzien.begin(), teletydzien.end(), compare_second); + int liczba_filmow = 0; + int i = 0, j = 1; + while (i < teletydzien.size()) { + liczba_filmow++; + while (teletydzien[j].first <= teletydzien[i].second && j < teletydzien.size()) + j++; + i = j++; + } + + return liczba_filmow; +} + +int main() { + std::cout << kinoman({{1, 5}, {2, 3}, {3, 6}, {4, 7}, {9, 11}}) << "\n"; +} \ No newline at end of file diff --git a/src/cw12/zad2/gwozde.cpp b/src/cw12/zad2/gwozde.cpp new file mode 100644 index 0000000..e31cd3f --- /dev/null +++ b/src/cw12/zad2/gwozde.cpp @@ -0,0 +1,4 @@ +/* +sortujemy po końcach odcinków + +*/ \ No newline at end of file diff --git a/src/cw12/zad3/stacje_benzynowe.cpp b/src/cw12/zad3/stacje_benzynowe.cpp new file mode 100644 index 0000000..412d6d5 --- /dev/null +++ b/src/cw12/zad3/stacje_benzynowe.cpp @@ -0,0 +1,3 @@ +/* +tankujemy na ostatniej możliwej stacji zanim skończy nam się paliwo +*/ \ No newline at end of file diff --git a/src/cw12/zad4/silnia.cpp b/src/cw12/zad4/silnia.cpp new file mode 100644 index 0000000..77fd6f1 --- /dev/null +++ b/src/cw12/zad4/silnia.cpp @@ -0,0 +1,77 @@ +/* + 5! = 4 * 4! + 3 * 3! + 2 * 2! + 1 * 1! + 1 + 5! = 4 * 24 + 18 + 4 + 1 + 1 +*/ + +#include +#include + +class Program { + std::vector factorials_map; + std::vector factorial_indexes; + + void set_factorials_map(int n) { + if (n < 0) + return; + + factorials_map.push_back(1); + int i = 0; + + while (factorials_map[i] < n) { + i++; + factorials_map.push_back(factorials_map[i - 1] * i); + } + + factorials_map.pop_back(); + } + + int get_highest_factorial(int n) { + int left = 0; + int right = factorials_map.size() - 1; + int result = -1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (factorials_map[mid] <= n) { + result = mid; + left = mid + 1; + } else { + right = mid - 1; + } + } + + return result; + } + + void factorials(int n) { + if (!n) + return; + + int i = get_highest_factorial(n); + factorial_indexes.push_back(i); + factorials(n - factorials_map[i]); + } + + void print() { + for (int factorial : factorial_indexes) + std::cout << factorial << " "; + std::cout << "\n"; + + for (int factorial : factorial_indexes) + std::cout << factorials_map[factorial] << " "; + std::cout << "\n"; + } + + public: + Program(int n) { + set_factorials_map(n); + factorials(n); + print(); + } +}; + +int main() { + Program(169); + Program(42); +} \ No newline at end of file diff --git a/src/cw9/zad2/monkeys.cpp b/src/cw9/zad2/monkeys.cpp new file mode 100644 index 0000000..53e691e --- /dev/null +++ b/src/cw9/zad2/monkeys.cpp @@ -0,0 +1,13 @@ +#include +#include + +int* monkeys(int** monkeys, int n) { + int* fallen_monkeys = new int[n]; + std::fill(fallen_monkeys, fallen_monkeys + n, -1); + // ustawiamy INT_MAX w małpce nr 1 i we wszystkich małpkach, które się jej trzymają i nie mają podanego czasu puszczenia + // +} + +int main() { + +} \ No newline at end of file diff --git a/src/cw9/zad5/path.cpp b/src/cw9/zad5/path.cpp index e69de29..2ae97c6 100644 --- a/src/cw9/zad5/path.cpp +++ b/src/cw9/zad5/path.cpp @@ -0,0 +1,95 @@ +#include +#include +#include + +class Graph { + public: + typedef std::pair edge; + + private: + int n_vertices = 0; + std::vector> edges{}; + + public: + Graph(int n) { + n_vertices = n; + for (int i = 0; i < n; i++) { + edges.push_back(std::vector{}); + } + } + int size() { + return n_vertices; + } + + void insert_directed_edge(int v1, int v2, float len) { + edges[v1].push_back(std::make_pair(v2, len)); + } + + void insert_directed_edge(int v1, int v2) { + insert_directed_edge(v1, v2, 1.0); + } + + void insert_edge(int v1, int v2, float len) { + insert_directed_edge(v1, v2, len); + insert_directed_edge(v2, v1, len); + } + + void insert_edge(int v1, int v2) { + insert_edge(v1, v2, 1.0); + } + + std::vector neighbours(int v) { + return edges[v]; + } + + std::vector> search() { + std::vector visited(size(), false); + std::vector> result{}; + for (int v = 0; v < n_vertices; v++) { + if (!visited[v]) { + std::deque q{v}; + std::vector component{}; + visited[v] = true; + while (!q.empty()) { + int u = q.front(); + q.pop_front(); + component.push_back(u); + for (std::pair e : neighbours(u)) { + int w = e.first; + if (!visited[w]) { + visited[w] = true; + q.push_back(w); + } + } + } + result.push_back(component); + } + } + return result; + } +}; + +class CompareDist { + public: + bool operator()(Graph::edge const &e1, Graph::edge const &e2) { + return e1.first > e2.first; + } +}; + +int get_longest_increasing_path_length(Graph g) { + int next_element; + bool *visited = new bool[g.size()]{false}; + // std::pair + std::priority_queue, CompareDist> path; + // minimal element: 0 + + for (int i = 0; i < g.size(); i++) { + if (visited[i]) + continue; + + path.push(std::make_pair(0, 0)); + while (!path.empty()) { + + } + } +} \ No newline at end of file diff --git a/src/kol3_13/mice_in_the_corridor.cpp b/src/kol3_13/mice_in_the_corridor.cpp index 71e5584..3126205 100644 --- a/src/kol3_13/mice_in_the_corridor.cpp +++ b/src/kol3_13/mice_in_the_corridor.cpp @@ -1,18 +1,27 @@ #include -#include #include +#include int** get_sum(int mice[], int n) { int** sum = new int*[n]; - for (int i = 0; i < n; i++) + for (int i = 0; i < n; i++) { sum[i] = new int[n]{0}; + sum[i][i] = mice[i]; + } + + int* prefix_sum = new int[n]{mice[0]}; + for (int i = 1; i < n; i++) + prefix_sum[i] = prefix_sum[i - 1] + mice[i]; + int* length_coefficient = new int[n]; for (int i = 0; i < n; i++) - sum[i][i] = mice[i]; - - for (int length = 2; length < n; length++) { - for (int i = 0; i < n - length; i++) { - sum[i][i + length - 1] = sum[i][i+ length - 2] + mice[i + length - 1] - pow(length - 1, 2); + length_coefficient[0] = pow(i + 1, 2); + + for (int length = 2; length <= n; length++) { + for (int i = 0; i < n - length + 2; i++) { + sum[i][i + length - 1] = std::max( + sum[i][i + length - 2] + mice[i + length - 1] + (int)pow(length - 2, 2) - (int)pow(length - 1, 2), + 0); } } @@ -20,26 +29,81 @@ int** get_sum(int mice[], int n) { } int** get_max_in_interval(int** sum, int n) { - + int** interval_max = new int*[n]; + for (int i = 0; i < n; i++) { + interval_max[i] = new int[n]{0}; + interval_max[i][i] = sum[i][i]; + } + + int j; + for (int length = 2; length <= n; length++) { + for (int i = 0; i < n - length + 1; i++) { + j = i + length - 1; + interval_max[i][j] = std::max( + sum[i][j], + std::max( + interval_max[i + 1][j], + interval_max[i][j - 1])); + } + } + + return interval_max; } +void delete_array(int** tab, int n, int m) { +} -void print(int** tab, int n) { +void print(int** tab, int n, int m) { for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) + for (int j = 0; j < m; j++) std::cout << tab[i][j] << "\t"; std::cout << "\n"; } } +int mice_in_the_corridor(int mice[], int n, int k) { + if (k < 1 || k > n) + return -1; + + int** dp = new int*[k - 1]; + for (int i = 0; i < k - 1; i++) { + dp[i] = new int[n - 1]{0}; + } + + int** interval_max = get_max_in_interval(get_sum(mice, n), n); + + for (int i = 0; i < n - 1; i++) { + dp[0][i] = interval_max[0][i]; + // dp[0][i] = interval_max[i + 1][n - 1]; + } -int mice_in_the_corridor(int mice[], int n) { + for (int cat = 2; cat < k; cat++) + dp[cat - 1][cat - 1] = dp[cat - 2][cat - 2] + mice[cat - 1]; + std::cout << "\n"; + + for (int cat = 2; cat < k; cat++) { + for (int i = cat - 1; i < n - 1; i++) { + // dp[cat - 1][i] = dp[cat - 2][i]; + // dp[cat - 1][i] = interval_max[i + 1][n - 1]; + } + // print(dp[cat - 1], 2, n); + std::cout << "\n"; + } + // print(dp, k - 1, n); + + return 1; } int main() { // print() + int n = 8; int* mice = new int[8]{1, 5, 1, 4, 3, 2, 7, 0}; - print(get_sum(mice, 8), 8); - + int** sum = get_sum(mice, n); + // print(sum, n, n); + std::cout << "\n"; + int** interval_max = get_max_in_interval(sum, n); + // print(interval_max, n, n); + std::cout << "\n"; + int res = mice_in_the_corridor(mice, n, 8); } \ No newline at end of file diff --git a/src/kol3_23/zad1/rabunek_1d.cpp b/src/kol3_23/zad1/rabunek_1d.cpp new file mode 100644 index 0000000..e5771e9 --- /dev/null +++ b/src/kol3_23/zad1/rabunek_1d.cpp @@ -0,0 +1,12 @@ +#include + +int* init_dp(int k) { + int* dp = new int[k + 1]; + std::fill(dp, dp + k + 1, -1); + return dp; +} + +int rabunek(std::vector n, int k) { + int* dp = init_dp(k); + +} \ No newline at end of file diff --git a/src/kol3_23/zad1/rabunek_2d.cpp b/src/kol3_23/zad1/rabunek_2d.cpp new file mode 100644 index 0000000..d59d9f5 --- /dev/null +++ b/src/kol3_23/zad1/rabunek_2d.cpp @@ -0,0 +1,19 @@ +#include + +bool** init_dp(int n_size, int k) { + bool** dp = new bool*[k+1]; + for (int i = 0; i <= k; i++) + dp[i] = new bool[n_size+1]{false}; + + dp[0][n_size] = true; + return dp; +} + +void delete_dp(bool** dp, int k) { + for (int i = 0; i <= k; i++) + delete[] dp[i]; + delete[] dp; +} + +int rabunek(std::vector n, int k) { +} \ No newline at end of file diff --git a/src/kol3_23/zad2/proximity_bfs.cpp b/src/kol3_23/zad2/proximity_bfs.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/other/BFS.cpp b/src/other/BFS.cpp new file mode 100644 index 0000000..b08f518 --- /dev/null +++ b/src/other/BFS.cpp @@ -0,0 +1,90 @@ +// C++ code to print BFS traversal from a given +// source vertex + +#include +using namespace std; + +// This class represents a directed graph using +// adjacency list representation +class Graph { + + // No. of vertices + int V; + + // Pointer to an array containing adjacency lists + vector > adj; + +public: + // Constructor + Graph(int V); + + // Function to add an edge to graph + void addEdge(int v, int w); + + // Prints BFS traversal from a given source s + void BFS(int s); +}; + +Graph::Graph(int V) +{ + this->V = V; + adj.resize(V); +} + +void Graph::addEdge(int v, int w) +{ + // Add w to v’s list. + adj[v].push_back(w); +} + +void Graph::BFS(int s) +{ + // Mark all the vertices as not visited + vector visited; + visited.resize(V, false); + + // Create a queue for BFS + list queue; + + // Mark the current node as visited and enqueue it + visited[s] = true; + queue.push_back(s); + + while (!queue.empty()) { + + // Dequeue a vertex from queue and print it + s = queue.front(); + cout << s << " "; + queue.pop_front(); + + // Get all adjacent vertices of the dequeued + // vertex s. + // If an adjacent has not been visited, + // then mark it visited and enqueue it + for (auto adjacent : adj[s]) { + if (!visited[adjacent]) { + visited[adjacent] = true; + queue.push_back(adjacent); + } + } + } +} + +// Driver code +int main() +{ + // Create a graph given in the above diagram + Graph g(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 0); + g.addEdge(2, 3); + g.addEdge(3, 3); + + cout << "Following is Breadth First Traversal " + << "(starting from vertex 2) \n"; + g.BFS(2); + + return 0; +} diff --git a/src/other/DFS.cpp b/src/other/DFS.cpp new file mode 100644 index 0000000..e921625 --- /dev/null +++ b/src/other/DFS.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +// This class represents a directed graph using adjacency +// list representation +class Graph +{ + int V; // No. of vertices + list *adj; // adjacency lists +public: + Graph(int V); // Constructor + void addEdge(int v, int w); // to add an edge to graph + void DFS(int s); // prints all vertices in DFS manner + // from a given source. +}; + +Graph::Graph(int V) +{ + this->V = V; + adj = new list[V]; +} + +void Graph::addEdge(int v, int w) +{ + adj[v].push_back(w); // Add w to v’s list. +} + +void Graph::DFS(int s) +{ + // Initially mark all vertices as not visited + vector visited(V, false); + + // Create a stack for DFS + stack stack; + + // Push the current source node. + stack.push(s); + + while (!stack.empty()) + { + // Pop a vertex from stack and print it + int s = stack.top(); + stack.pop(); + + // Stack may contain same vertex twice. So + // we need to print the popped item only + // if it is not visited. + if (!visited[s]) + { + cout << s << " "; + visited[s] = true; + } + + // Get all adjacent vertices of the popped vertex s + // If a adjacent has not been visited, then push it + // to the stack. + for (auto i = adj[s].begin(); i != adj[s].end(); ++i) + if (!visited[*i]) + stack.push(*i); + } +} \ No newline at end of file diff --git a/src/other/DFS.cs b/src/other/DFS.cs new file mode 100644 index 0000000..852db70 --- /dev/null +++ b/src/other/DFS.cs @@ -0,0 +1,84 @@ +// C# program to print DFS traversal +// from a given graph +using System; +using System.Collections.Generic; + +// This class represents a directed graph +// using adjacency list representation +class Graph { + private int V; + + // Array of lists for + // Adjacency List Representation + private List[] adj; + + // Constructor + Graph(int v) + { + V = v; + adj = new List[ v ]; + for (int i = 0; i < v; ++i) + adj[i] = new List(); + } + + // Function to Add an edge into the graph + void AddEdge(int v, int w) + { + // Add w to v's list. + adj[v].Add(w); + } + + // A function used by DFS + void DFSUtil(int v, bool[] visited) + { + // Mark the current node as visited + // and print it + visited[v] = true; + Console.Write(v + " "); + + // Recur for all the vertices + // adjacent to this vertex + List vList = adj[v]; + foreach(var n in vList) + { + if (!visited[n]) + DFSUtil(n, visited); + } + } + + // The function to do DFS traversal. + // It uses recursive DFSUtil() + void DFS(int v) + { + // Mark all the vertices as not visited + // (set as false by default in c#) + bool[] visited = new bool[V]; + + // Call the recursive helper function + // to print DFS traversal + DFSUtil(v, visited); + } + + // Driver Code + public static void Main(String[] args) + { + Graph g = new Graph(4); + + g.AddEdge(0, 1); + g.AddEdge(0, 2); + g.AddEdge(1, 2); + g.AddEdge(2, 0); + g.AddEdge(2, 3); + g.AddEdge(3, 3); + + Console.WriteLine( + "Following is Depth First Traversal " + + "(starting from vertex 2)"); + + // Function call + g.DFS(2); + Console.ReadKey(); + } +} + +// This code is contributed by techno2mahi diff --git a/src/other/DFS_recurr.cpp b/src/other/DFS_recurr.cpp new file mode 100644 index 0000000..2536940 --- /dev/null +++ b/src/other/DFS_recurr.cpp @@ -0,0 +1,90 @@ +// An Iterative C++ program to do DFS traversal from +// a given source vertex. DFS(int s) traverses vertices +// reachable from s. +#include +using namespace std; + +// This class represents a directed graph using adjacency +// list representation +class Graph +{ + int V; // No. of vertices + list *adj; // adjacency lists +public: + Graph(int V); // Constructor + void addEdge(int v, int w); // to add an edge to graph + void DFS(); // prints all vertices in DFS manner + + // prints all not yet visited vertices reachable from s + void DFSUtil(int s, vector &visited); +}; + +Graph::Graph(int V) +{ + this->V = V; + adj = new list[V]; +} + +void Graph::addEdge(int v, int w) +{ + adj[v].push_back(w); // Add w to v’s list. +} + +// prints all not yet visited vertices reachable from s +void Graph::DFSUtil(int s, vector &visited) +{ + // Create a stack for DFS + stack stack; + + // Push the current source node. + stack.push(s); + + while (!stack.empty()) + { + // Pop a vertex from stack and print it + int s = stack.top(); + stack.pop(); + + // Stack may contain same vertex twice. So + // we need to print the popped item only + // if it is not visited. + if (!visited[s]) + { + cout << s << " "; + visited[s] = true; + } + + // Get all adjacent vertices of the popped vertex s + // If a adjacent has not been visited, then push it + // to the stack. + for (auto i = adj[s].begin(); i != adj[s].end(); ++i) + if (!visited[*i]) + stack.push(*i); + } +} + +// prints all vertices in DFS manner +void Graph::DFS() +{ + // Mark all the vertices as not visited + vector visited(V, false); + + for (int i = 0; i < V; i++) + if (!visited[i]) + DFSUtil(i, visited); +} + +// Driver program to test methods of graph class +int main() +{ + Graph g(5); // Total 5 vertices in graph + g.addEdge(1, 0); + g.addEdge(2, 1); + g.addEdge(3, 4); + g.addEdge(4, 0); + + cout << "Following is Depth First Traversal\n"; + g.DFS(); + + return 0; +} diff --git a/src/other/dijkstra.cpp b/src/other/dijkstra.cpp new file mode 100644 index 0000000..70361de --- /dev/null +++ b/src/other/dijkstra.cpp @@ -0,0 +1,37 @@ +/* + T(n, m) = Θ((n + m) logn) + M(n, m) = Θ(n + m). + + n - vertices + m - edges +*/ + +class CompareDist { + public: + bool operator()(edge const &e1, edge const &e2) { + if (e1.second > e2.second) return true; + if (e1.second < e2.second) return false; + return e1.first > e2.first; + } +}; + +vector dijkstra(int v) { + vector visited(size(), false); + vector dist(size(), HUGE_VALF); + priority_queue, CompareDist> q; + q.push(make_pair(v, 0.0)); + while (!q.empty()) { + edge p = q.top(); + int u = p.first; + float d = p.second; + q.pop(); + if (!visited[u]) { + dist[u] = d; + visited[u] = true; + for (edge e : neighbours(u)) { + q.push(make_pair(e.first, e.second + d)); + } + } + } + return dist; +} \ No newline at end of file