diff --git a/3001_daniel-diaz/3001_daniel-diaz_v1.cpp b/3001_daniel-diaz/3001_daniel-diaz_v1.cpp new file mode 100644 index 0000000..62db9af --- /dev/null +++ b/3001_daniel-diaz/3001_daniel-diaz_v1.cpp @@ -0,0 +1,215 @@ +// Made by Daniel Diaz (@Danidiaztech) +#include +using namespace std; + +tuple bubble_sort(vector &a){ + int n = a.size(); + int comps = 0; + int swaps = 0; + + for (int i =0 ;i < n; i++){ + for (int j = 0; j < n - i - 1; j++){ + if (a[j] > a[j + 1]){ + swap(a[j], a[j + 1]); + swaps++; + } + comps++; + } + } + + return {comps, swaps}; +} + + +tuple bubble_sort_wrapper(vector &a){ + /** + * @brief + * Sorts a vector of long longs in non-descending order + * Time complexity: O(n log n) + * Space complexity: O(n log n) + */ + const auto start{chrono::steady_clock::now()}; + auto [comps, swaps] = bubble_sort(a); + + const auto finish{chrono::steady_clock::now()}; + const chrono::duration elapsed{finish - start}; + + return {"Bubble sort", comps, swaps, elapsed.count()}; +} + +tuple merge_sort(int l, int r, vector &a){ + if (l == r){ + return {0,0}; + } + + int m = l + (r - l) / 2 ; + + + auto [c1, s1] = merge_sort(l, m, a); + auto [c2, s2] = merge_sort(m + 1, r, a); + + + long long comps = c1 + c2; + long long swaps = s1 + s2; + + vector left, right; + + for (int i = l ; i <= m; i++) left.push_back(a[i]); + for (int i = m + 1 ; i <= r; i++) right.push_back(a[i]); + + int i =0, j =0 ; + + int ls = left.size(); + int rs = right.size(); + + + while (i < ls && j < rs){ + if (left[i] > right[j]){ + a[i + j + l] = right[j]; + + j++; + swaps++; + } + else{ + a[i + j + l] = left[i]; + i++; + } + + comps++; + } + + while (i < ls){ + a[i + j + l ] = left[i]; + + i++; + } + while (j < rs){ + a[i + j + l ] = right[j]; + + j++; + } + + + return {comps, swaps}; + +} + + +tuple merge_sort_wrapper(vector &a){ + /** + * @brief + * Sorts a vector of long longs in non-descending order + * Time complexity: O(n^2) + * Space complexity: O(1) + */ + const auto start{chrono::steady_clock::now()}; + + auto [comps, swaps] = merge_sort(0, (int)a.size() - 1, a); + + const auto finish{chrono::steady_clock::now()}; + const chrono::duration elapsed{finish - start}; + + return {"Merge sort", comps, swaps, elapsed.count()}; +} + +void solve(){ + vector a; + + cout << "3001 - Sort benchmark: " << endl; + cout << "Mode A : Input N (size of array), and N elements - Type A in first line" << endl; + cout << "Mode B : Input N (size of array), and seed - Type B in first line" << endl; + string mode; cin >> mode; + + if (mode == "A"){ + int n; cin >> n; + for (int i = 0;i < n ; i++){ + long long x; cin >> x; + a.push_back(x); + } + } + else{ + int n, seed; cin >> n >> seed; + long long lim = 1e12; + + mt19937 gen(seed); // mersenne_twister_engine seeded seed from input + uniform_int_distribution distrib(-lim, lim); + + for (int i =0;i < n; i++){ + a.push_back(distrib(gen)); + } + } + + auto a_bubble = a; + auto a_merge = a; + + auto [name1, comp1, swaps1, time1] = bubble_sort_wrapper(a_bubble); + auto [name2, comp2, swaps2, time2] = merge_sort_wrapper(a_merge); + time1 *= 1000.0; + time2 *= 1000.0; + + + // compare results + sort(a.begin(), a.end()); + + bool ok_bubble = a == a_bubble; + bool ok_merge = a == a_merge; + + + if (!ok_bubble){ + cout << "Failed bubble sort: Revise implementation" << endl; + } + if (!ok_merge){ + cout << "Failed merge sort: Revise implementation" << endl; + } + + if (ok_bubble && ok_merge){ + cout << name1 << ", " << "comps=" << comp1 << ", swaps=" << swaps1 << ", ms=" << time1 << endl; + cout << name2 << ", " << "comps=" << comp2 << ", swaps=" << swaps2 << ", ms=" << time2 << endl; + } + +} + +int main() { + cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); + + #if LOCAL + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + int tc = 1; + // cin >> tc; + + for (int t = 1; t <= tc; t++){ + solve(); + } +} + +/** +TESTCASE 1 +IN: +B +10000 1 +OUT: +Bubble sort, comps=49995000, swaps=25131616, ms=749.669 +Merge sort, comps=120562, swaps=59096, ms=7.19629 + + +TESTCASE 2 +IN: +B +10 1 + +OUT: +Bubble sort, comps=45, swaps=19, ms=0.000951 +Merge sort, comps=22, swaps=9, ms=0.00549 + +TESTCASE 3 +IN: +A +10 +10 9 8 7 6 5 4 3 2 1 + +OUT: +Bubble sort, comps=45, swaps=45, ms=0.001569 +Merge sort, comps=15, swaps=15, ms=0.007354 +**/ \ No newline at end of file diff --git a/3001_daniel-diaz/input.txt b/3001_daniel-diaz/input.txt new file mode 100644 index 0000000..040c0b5 --- /dev/null +++ b/3001_daniel-diaz/input.txt @@ -0,0 +1,7 @@ +5 4 +1 2 +2 3 +3 4 +4 5 + +1 5 diff --git a/3001_daniel-diaz/output.txt b/3001_daniel-diaz/output.txt new file mode 100644 index 0000000..050adce --- /dev/null +++ b/3001_daniel-diaz/output.txt @@ -0,0 +1,5 @@ +3001 - Sort benchmark: +Mode A : Input N (size of array), and N elements - Type A in first line +Mode B : Input N (size of array), and seed - Type B in first line +Bubble sort, comps=45, swaps=45, ms=0.001569 +Merge sort, comps=15, swaps=15, ms=0.007354 diff --git a/3001_daniel-diaz/run.sh b/3001_daniel-diaz/run.sh new file mode 100755 index 0000000..1d92a0f --- /dev/null +++ b/3001_daniel-diaz/run.sh @@ -0,0 +1 @@ +g++ -Wall -Wextra -DLOCAL -std=c++17 3001_daniel-diaz_v1.cpp -o main && ./main && rm main diff --git a/3002_daniel-diaz/3002_daniel-diaz_v1.cpp b/3002_daniel-diaz/3002_daniel-diaz_v1.cpp new file mode 100644 index 0000000..514eb6f --- /dev/null +++ b/3002_daniel-diaz/3002_daniel-diaz_v1.cpp @@ -0,0 +1,300 @@ +// Made by Daniel Diaz (@Danidiaztech) +#include +using namespace std; + + +void solve(){ + // TOTAL COMPLEXITY: O(Q * log(N)) + int N, Q; + cin >> N; + long long a[N]; + for (int i =0 ;i < N; i++){ + cin >> a[i]; + } + + cin >> Q; + + while (Q--){ + long long value; + cin >> value; + + // lower bound implementation + // min i such that: + // ai >= value + + long long l = -1, r = N; + + while (l + 1ll < r){ + long long m = l + (r - l) / 2; + + if (a[m] > value){ + r = m; + } + else{ + l = m; + } + } + + // l has lower_bound, r is first element greater than value + cout << l << endl; + } + +} + +int main() { + cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); + + #if LOCAL + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + + int tc = 1; + // cin >> tc; + + for (int t = 1; t <= tc; t++){ + solve(); + } +} + + + +/** +TESTCASE 1 +IN: +10 +-19 -7 -4 0 2 7 7 7 13 15 +5 +-7 7 7 2 15 +OUT: +1 +7 +7 +4 +9 + +TESTCASE 2: +IN: +30 +7 16 17 18 18 18 24 35 36 39 41 41 42 44 50 50 57 63 68 75 76 78 82 84 84 88 88 90 94 95 +5 +33 49 80 84 90 + +OUT: +6 +13 +21 +24 +27 + + +TESTCASE 3: +IN: +100 +0 1 2 6 8 11 11 12 12 13 14 15 15 16 17 18 18 20 22 24 28 29 29 30 30 30 30 30 31 31 31 33 34 34 35 35 36 36 37 37 37 37 40 40 40 41 42 43 45 45 47 48 51 51 52 53 53 53 55 55 58 58 58 59 59 61 61 63 63 64 64 65 65 66 68 69 71 72 73 75 79 80 82 82 82 83 83 85 85 86 88 88 88 93 95 95 97 98 99 99 +200 +1 1 1 1 2 4 5 6 6 6 6 7 7 7 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 12 13 14 14 14 15 15 15 15 16 16 17 17 17 17 18 18 19 20 22 23 23 24 25 25 25 26 26 27 27 28 30 30 31 31 32 34 35 35 36 37 38 38 38 40 41 42 42 43 43 43 44 45 45 45 46 46 46 47 47 47 47 48 48 49 50 50 52 52 53 53 54 55 55 55 56 56 56 56 57 57 57 57 58 58 58 59 59 59 59 59 60 60 61 62 62 62 62 63 63 64 65 65 66 66 66 66 67 69 69 69 70 70 70 70 71 71 72 72 73 75 75 75 75 76 79 80 80 80 81 81 82 82 83 83 84 85 86 86 86 87 87 89 90 90 90 90 90 90 91 91 92 92 92 92 92 94 94 95 96 96 96 96 97 97 98 98 99 99 99 100 + +OUT: +1 +1 +1 +1 +2 +2 +2 +3 +3 +3 +3 +3 +3 +3 +4 +4 +4 +4 +4 +4 +6 +6 +6 +6 +6 +8 +8 +8 +8 +8 +9 +10 +10 +10 +12 +12 +12 +12 +13 +13 +14 +14 +14 +14 +16 +16 +16 +17 +18 +18 +18 +19 +19 +19 +19 +19 +19 +19 +19 +20 +27 +27 +30 +30 +30 +33 +35 +35 +37 +41 +41 +41 +41 +44 +45 +46 +46 +47 +47 +47 +47 +49 +49 +49 +49 +49 +49 +50 +50 +50 +50 +51 +51 +51 +51 +51 +54 +54 +57 +57 +57 +59 +59 +59 +59 +59 +59 +59 +59 +59 +59 +59 +62 +62 +62 +64 +64 +64 +64 +64 +64 +64 +66 +66 +66 +66 +66 +68 +68 +70 +72 +72 +73 +73 +73 +73 +73 +75 +75 +75 +75 +75 +75 +75 +76 +76 +77 +77 +78 +79 +79 +79 +79 +79 +80 +81 +81 +81 +81 +81 +84 +84 +86 +86 +86 +88 +89 +89 +89 +89 +89 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +93 +93 +95 +95 +95 +95 +95 +96 +96 +97 +97 +99 +99 +99 +99 + + */ diff --git a/3002_daniel-diaz/input.txt b/3002_daniel-diaz/input.txt new file mode 100644 index 0000000..1419569 --- /dev/null +++ b/3002_daniel-diaz/input.txt @@ -0,0 +1,4 @@ +100 +0 1 2 6 8 11 11 12 12 13 14 15 15 16 17 18 18 20 22 24 28 29 29 30 30 30 30 30 31 31 31 33 34 34 35 35 36 36 37 37 37 37 40 40 40 41 42 43 45 45 47 48 51 51 52 53 53 53 55 55 58 58 58 59 59 61 61 63 63 64 64 65 65 66 68 69 71 72 73 75 79 80 82 82 82 83 83 85 85 86 88 88 88 93 95 95 97 98 99 99 +200 +1 1 1 1 2 4 5 6 6 6 6 7 7 7 9 9 10 10 10 10 11 11 11 11 11 12 12 12 12 12 13 14 14 14 15 15 15 15 16 16 17 17 17 17 18 18 19 20 22 23 23 24 25 25 25 26 26 27 27 28 30 30 31 31 32 34 35 35 36 37 38 38 38 40 41 42 42 43 43 43 44 45 45 45 46 46 46 47 47 47 47 48 48 49 50 50 52 52 53 53 54 55 55 55 56 56 56 56 57 57 57 57 58 58 58 59 59 59 59 59 60 60 61 62 62 62 62 63 63 64 65 65 66 66 66 66 67 69 69 69 70 70 70 70 71 71 72 72 73 75 75 75 75 76 79 80 80 80 81 81 82 82 83 83 84 85 86 86 86 87 87 89 90 90 90 90 90 90 91 91 92 92 92 92 92 94 94 95 96 96 96 96 97 97 98 98 99 99 99 100 \ No newline at end of file diff --git a/3002_daniel-diaz/output.txt b/3002_daniel-diaz/output.txt new file mode 100644 index 0000000..1c34516 --- /dev/null +++ b/3002_daniel-diaz/output.txt @@ -0,0 +1,200 @@ +1 +1 +1 +1 +2 +2 +2 +3 +3 +3 +3 +3 +3 +3 +4 +4 +4 +4 +4 +4 +6 +6 +6 +6 +6 +8 +8 +8 +8 +8 +9 +10 +10 +10 +12 +12 +12 +12 +13 +13 +14 +14 +14 +14 +16 +16 +16 +17 +18 +18 +18 +19 +19 +19 +19 +19 +19 +19 +19 +20 +27 +27 +30 +30 +30 +33 +35 +35 +37 +41 +41 +41 +41 +44 +45 +46 +46 +47 +47 +47 +47 +49 +49 +49 +49 +49 +49 +50 +50 +50 +50 +51 +51 +51 +51 +51 +54 +54 +57 +57 +57 +59 +59 +59 +59 +59 +59 +59 +59 +59 +59 +59 +62 +62 +62 +64 +64 +64 +64 +64 +64 +64 +66 +66 +66 +66 +66 +68 +68 +70 +72 +72 +73 +73 +73 +73 +73 +75 +75 +75 +75 +75 +75 +75 +76 +76 +77 +77 +78 +79 +79 +79 +79 +79 +80 +81 +81 +81 +81 +81 +84 +84 +86 +86 +86 +88 +89 +89 +89 +89 +89 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +92 +93 +93 +95 +95 +95 +95 +95 +96 +96 +97 +97 +99 +99 +99 +99 diff --git a/3002_daniel-diaz/run.sh b/3002_daniel-diaz/run.sh new file mode 100755 index 0000000..736b7bb --- /dev/null +++ b/3002_daniel-diaz/run.sh @@ -0,0 +1 @@ +g++ -Wall -Wextra -DLOCAL -std=c++17 3002_daniel-diaz_v1.cpp -o main && ./main && rm main \ No newline at end of file diff --git a/3003_daniel-diaz/3003_daniel-diaz_v1.cpp b/3003_daniel-diaz/3003_daniel-diaz_v1.cpp new file mode 100644 index 0000000..d77c017 --- /dev/null +++ b/3003_daniel-diaz/3003_daniel-diaz_v1.cpp @@ -0,0 +1,120 @@ +// Made by Daniel Diaz (@Danidiaztech) +#include +using namespace std; + +#define ll long long + +const int mod = 1e9 + 7; +const string yes = "YES", no = "NO"; + +void solve(){ + int V, E; + cin >> V >> E; + vector> g(V + 1); + vector parent(V + 1, - 1), dis(V +1 , -1); + + for (int i =0 ;i < E; i++){ + int u, v; cin >> u >> v; + g[u].push_back(v); + g[v].push_back(u); + } + int s, t; cin >> s >> t; + queue q; + q.push(s); + parent[s] = 0; + dis[s] = 0; + + // BFS + while (q.size()){ + int u = q.front(); + q.pop(); + + for (auto v: g[u]){ + if (parent[v] == -1){ + parent[v] = u; + dis[v] = dis[u] + 1; + q.push(v); + } + } + } + + if (parent[t] == -1){ + cout << no << endl; + return; + } + + vector path; + int x = t; + while (x != 0){ + path.push_back(x); + x = parent[x]; + } + + cout << "dist=" << dis[t] << endl; + + reverse(path.begin(), path.end()); + + for (int i =0 ;i < path.size(); i++){ + if (i > 0){ + cout << "->"; + } + cout << path[i]; + } + cout << endl; + +} + +int main() { + cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); + + #if LOCAL + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + + int tc = 1; + // cin >> tc; + + for (int t = 1; t <= tc; t++){ + solve(); + } +} + +/** +TESTCASE 1 +IN: +5 4 +1 2 +2 3 +3 4 +4 5 +1 5 +OUT: +dist=4 +1->2->3->4->5 + +TESTCASE 2 +IN: +6 7 +1 2 +1 3 +2 4 +3 4 +4 5 +5 6 +2 6 +1 6 +OUT: +dist=2 +1->2->6 + +TESTCASE 3 +IN: +4 2 +1 2 +3 4 +1 4 + +OUT: +NO +**/ \ No newline at end of file diff --git a/3003_daniel-diaz/input.txt b/3003_daniel-diaz/input.txt new file mode 100644 index 0000000..5e61052 --- /dev/null +++ b/3003_daniel-diaz/input.txt @@ -0,0 +1,4 @@ +4 2 +1 2 +3 4 +1 4 diff --git a/3003_daniel-diaz/output.txt b/3003_daniel-diaz/output.txt new file mode 100644 index 0000000..5e35d1b --- /dev/null +++ b/3003_daniel-diaz/output.txt @@ -0,0 +1 @@ +NO diff --git a/3003_daniel-diaz/run.sh b/3003_daniel-diaz/run.sh new file mode 100755 index 0000000..8bfac7e --- /dev/null +++ b/3003_daniel-diaz/run.sh @@ -0,0 +1 @@ +g++ -Wall -Wextra -DLOCAL -std=c++17 3003_daniel-diaz_v1.cpp -o main && ./main && rm main diff --git a/3004_daniel-diaz/3004_daniel-diaz_v1.cpp b/3004_daniel-diaz/3004_daniel-diaz_v1.cpp new file mode 100644 index 0000000..4fb71ff --- /dev/null +++ b/3004_daniel-diaz/3004_daniel-diaz_v1.cpp @@ -0,0 +1,124 @@ +// Made by Daniel Diaz (@Danidiaztech) +#include +using namespace std; + +#define ll long long + +const int mod = 1e9 + 7; +const string yes = "YES", no = "NO"; + +void solve(){ + int V, E; + cin >> V >> E; + vector> g(V + 1); + vector parent(V + 1, - 1), in(V + 1, 0); + vector vis(V +1 , 0); + + for (int i =0 ;i < E; i++){ + int u, v; cin >> u >> v; + in[v]++; + g[u].push_back(v); + } + + bool cycle = 0 ; + function dfs = [&](int u){ + if (cycle) return; + vis[u] = 1; + for (auto v: g[u]){ + if (cycle) return; + if (vis[v]){ + cycle = 1; + return; + } + } + }; + + // assumming values of V are from 1 to V and G is connected + dfs(1); + + if (cycle){ + cout << "CYCLE" << endl; + return; + } + + + queue q; + for (int i =1 ; i <= V; i++){ + if (in[i] == 0) q.push(i); + } + + vector topo; + + while (q.size()){ + int u = q.front(); + topo.push_back(u); + q.pop(); + + for (auto v: g[u]){ + in[v]--; + if (in[v] ==0 ){ + q.push(v); + } + } + } + + cout << "topo: " ; + for (auto x: topo) cout << x << " "; + cout << endl; + +} + +int main() { + cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); + + #if LOCAL + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); + #endif + + int tc = 1; + // cin >> tc; + + for (int t = 1; t <= tc; t++){ + solve(); + } +} + +/** +TESTCASE 1 +IN: +5 4 +1 2 +2 3 +3 4 +4 5 +1 5 +OUT: +dist=4 +1->2->3->4->5 + +TESTCASE 2 +IN: +6 7 +1 2 +1 3 +2 4 +3 4 +4 5 +5 6 +2 6 +1 6 +OUT: +dist=2 +1->2->6 + +TESTCASE 3 +IN: +4 2 +1 2 +3 4 +1 4 + +OUT: +NO +**/ \ No newline at end of file diff --git a/3004_daniel-diaz/input.txt b/3004_daniel-diaz/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/3004_daniel-diaz/output.txt b/3004_daniel-diaz/output.txt new file mode 100644 index 0000000..e69de29 diff --git a/helper/gen_tests/gen_array.py b/helper/gen_tests/gen_array.py new file mode 100755 index 0000000..10bcf4f --- /dev/null +++ b/helper/gen_tests/gen_array.py @@ -0,0 +1,29 @@ +import sys +import random + +n = int(sys.argv[1]) # Number of elements + + +l = 0 +r = 1e9 + +to_sort = 0 + +if (len(sys.argv) >= 4): + # Read L and R + l = int(sys.argv[2]) + r = int(sys.argv[3]) + +if (len(sys.argv) == 5): + to_sort = bool(sys.argv[4] == "1") + +arr = [] +for i in range(n): + arr.append(random.randint(l, r)) + +if (to_sort): + arr.sort() + +print(n) +for x in arr: + print(x, end=" ") \ No newline at end of file