diff --git a/Algoritms/Olympiada 2023-2024/Anton_and_watermelons.cpp b/Algoritms/Olympiada 2023-2024/Anton_and_watermelons.cpp new file mode 100644 index 0000000..284df89 --- /dev/null +++ b/Algoritms/Olympiada 2023-2024/Anton_and_watermelons.cpp @@ -0,0 +1,48 @@ +// Copyright 2022 Zener +// Шаблон для олимпиадных задач + +#include + +using namespace std; + +void solve(); + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("error.txt", "w", stderr); + freopen("output.txt", "w", stdout); +#endif + + int t = 1; + // /*is Single Test case?*/ cin >> t; + while (t--) { + solve(); + cout << "\n"; + } + + cerr << "time taken : " << (float) clock() / CLOCKS_PER_SEC << " secs" << endl; + return 0; +} + +void solve() { + long long n, m, d, temp; + cin >> n >> m >> d; + vector vec_x; + vector vec_y; + for(int i = 0; i < d; ++i){ + cin >> temp; + vec_x.push_back(temp); + cin >> temp; + vec_y.push_back(temp); + } + long long x = vec_x[0], y = vec_y[0]; + for(int i = 0; i < d; ++i){ + x = min(x, vec_x[i]); + y = min(y, vec_y[i]); + } + cout << x * y << " " << d; +} diff --git a/Algoritms/Olympiada 2023-2024/Colored_cube.cpp b/Algoritms/Olympiada 2023-2024/Colored_cube.cpp new file mode 100644 index 0000000..2e5bb26 --- /dev/null +++ b/Algoritms/Olympiada 2023-2024/Colored_cube.cpp @@ -0,0 +1,46 @@ +// Copyright 2022 Zener +// Шаблон для олимпиадных задач + +#include + +using namespace std; + +void solve(); + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("error.txt", "w", stderr); + freopen("output.txt", "w", stdout); +#endif + + int t = 1; + // /*is Single Test case?*/ cin >> t; + while (t--) { + solve(); + cout << "\n"; + } + + cerr << "time taken : " << (float) clock() / CLOCKS_PER_SEC << " secs" << endl; + return 0; +} + +void solve() { + int n, f; + cin >> n >> f; + if(f == 0){ + cout << (n - 2) * (n - 2); + } + else if(f == 1){ + cout << (n - 2) * (n - 2) * 6; + } + else if(f == 2){ + cout << (n - 2) * 12; + } + else if(f == 3){ + cout << 8; + } +} diff --git a/Algoritms/Olympiada 2023-2024/Triangle_of_sticks.cpp b/Algoritms/Olympiada 2023-2024/Triangle_of_sticks.cpp new file mode 100644 index 0000000..aa8ac63 --- /dev/null +++ b/Algoritms/Olympiada 2023-2024/Triangle_of_sticks.cpp @@ -0,0 +1,45 @@ +// Copyright 2022 Zener +// Шаблон для олимпиадных задач + +#include + +using namespace std; + +void solve(); + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("error.txt", "w", stderr); + freopen("output.txt", "w", stdout); +#endif + + int t = 1; + // /*is Single Test case?*/ cin >> t; + while (t--) { + solve(); + cout << "\n"; + } + + cerr << "time taken : " << (float) clock() / CLOCKS_PER_SEC << " secs" << endl; + return 0; +} + +void solve() { + long long a, b, c; + cin >> a >> b >> c; + long long x; + x = max(max(a, b), c); + if(a == x){ + cout << b + c - a; + } + else if(b == x){ + cout << a + c - b; + } + else{ + cout << a + b - c; + } +} diff --git a/Algoritms/Olympiada 2023-2024/paid_musical_services.cpp b/Algoritms/Olympiada 2023-2024/paid_musical_services.cpp new file mode 100644 index 0000000..e06c103 --- /dev/null +++ b/Algoritms/Olympiada 2023-2024/paid_musical_services.cpp @@ -0,0 +1,45 @@ +// Copyright 2022 Zener +// Шаблон для олимпиадных задач + +#include + +using namespace std; + +void solve(); + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("error.txt", "w", stderr); + freopen("output.txt", "w", stdout); +#endif + + int t = 1; + // /*is Single Test case?*/ cin >> t; + while (t--) { + solve(); + cout << "\n"; + } + + cerr << "time taken : " << (float) clock() / CLOCKS_PER_SEC << " secs" << endl; + return 0; +} + +void solve() { + long long n, m; + cin >> n >> m; + long long a = 1, d = 1, ans = 0; + while(d <= m){ + if((n - 1) * d <= m){ + ans += m - (n - 1) * d; + d += 1; + } + else{ + d += 1; + } + } + cout << ans; +} diff --git a/Data_Structures/Graph/Graph.cpp b/Data_Structures/Graph/Graph.cpp new file mode 100644 index 0000000..05a734b --- /dev/null +++ b/Data_Structures/Graph/Graph.cpp @@ -0,0 +1,71 @@ +// +// Created by User on 15.10.2023. +// + +#include +#include "Graph.h" + + +int main() { + #ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("error.txt", "w", stderr); + freopen("output.txt", "w", stdout); + #endif + + // Пару городов + string msk = "Moscow"; + string piter = "Saint Petersburg"; + string kzn = "Kazan"; + string inno = "Innopolis"; + string ekat = "Ekaterinburg"; + string vlad = "Vladivostok"; + string pskov = "Pskov"; + + Graph g; + + g.add_vertex(msk); + g.add_vertex(piter); + g.add_vertex(kzn); + g.add_vertex(inno); + g.add_vertex(ekat); + g.add_vertex(vlad); + g.add_vertex(pskov); + + g.add_edge(msk, piter); + g.add_edge(msk, kzn); + g.add_edge(piter, kzn); + g.add_edge(kzn, inno); + g.add_edge(kzn, ekat); + g.add_edge(ekat, vlad); + g.add_edge(vlad, pskov); + g.add_edge(msk, pskov); + + g.edges()[0].length = 400; + g.edges()[1].length = 680; + g.edges()[2].length = 1200; + g.edges()[3].length = 42; + // Просто должно работать "Moscow Piter" + for (string city : g.path(msk, piter)) { + cout << city << " "; + } + cout << "\n"; + + // Проверка, сможет ли метод вернуть больше одного города. "Moscow Kazan Innopolis" + for (string city : g.path(msk, inno)) { + cout << city << " "; + } + cout << "\n"; + + // Проверка, сможет ли метод найти путь покороче. Правильный ответ: "Saint Petersburg Moscow Kazan" + for (string city : g.path(piter, kzn)) { + cout << city << " "; + } + cout << "\n"; + + vector cities = g.path(piter, pskov); + for (string city : cities) { + cout << city << " "; + } + cout << "\n"; +} diff --git a/Data_Structures/Graph/Graph.h b/Data_Structures/Graph/Graph.h new file mode 100644 index 0000000..c1f6093 --- /dev/null +++ b/Data_Structures/Graph/Graph.h @@ -0,0 +1,151 @@ +// +// Created by User on 15.10.2023. +// + +#ifndef COMPETITIVE_PROGRAMMING_GRAPH_H +#define COMPETITIVE_PROGRAMMING_GRAPH_H + +#include "Graph_Interface.h" +#include +#include + +using namespace std; +typedef pair psi; + +struct Road { + string city1; + string city2; + int length; + Road(string city1, string city2, int length) { + this->city1 = city1; + this->city2 = city2; + this->length = length; + } + Road(string city1, string city2) { + this->city1 = city1; + this->city2 = city2; + this->length = 1; + } +}; + +template +class Graph : public GraphInterface { + vector _vertexes; + vector _edges; + + public: + Graph() = default; + + vector vertexes() override { + return _vertexes; + } + + vector edges() override { + return _edges; + } + + void add_vertex(V vertex) override { + _vertexes.push_back(vertex); + } + + void add_edge(V v1, V v2) override { + _edges.push_back(Road(v1, v2)); + } + + void remove_vertex(V vertex) override { + auto index = find(_vertexes.begin(), _vertexes.end(), vertex); + if (index != _vertexes.end()) _vertexes.erase(index); + } + + void remove_edge(Road edge) override { +// auto index = find(_edges.begin(), _edges.end(), edge); +// _edges.erase(index); + } + + vector djkst(V v1, V v2) { + unordered_map> paths; + vector start(1, v1); + paths[v1] = start; + + unordered_set path_exist; + path_exist.insert(v1); + + queue next; + next.push(v1); + + unordered_set visited; + + while (!next.empty()) { + V actual = next.front(); + next.pop(); + + if (visited.contains(actual)) continue; + visited.insert(actual); + + for (Road edge : this->_edges) { + if (edge.city1 == actual) { + V neigh = edge.city2; + + if (!path_exist.contains(neigh) || paths[neigh].size() > paths[actual].size() + 1) { + paths[neigh] = paths[actual]; + paths[neigh].push_back(neigh); + next.push(neigh); + if (visited.contains(neigh)) visited.erase(neigh); + if (!path_exist.contains(neigh)) path_exist.insert(neigh); + } + } else if (edge.city2 == actual) { + V neigh = edge.city1; + + if (!path_exist.contains(neigh) || paths[neigh].size() > paths[actual].size() + 1) { + paths[neigh] = paths[actual]; + paths[neigh].push_back(neigh); + next.push(neigh); + if (visited.contains(neigh)) visited.erase(neigh); + if (!path_exist.contains(neigh)) path_exist.insert(neigh); + } + } + } + } + return paths[v2]; + } + + vector dfs(V actual, V v2, set visited, vector path) { + path.push_back(actual); + if (actual == v2) return path; + + if (visited.contains(actual)) { + path.pop_back(); + return path; + } + + visited.insert(actual); + + for (Road edge : this->_edges) { + if (edge.city1 == actual) { + vector new_path = dfs(edge.city2, v2, visited, path); + if (new_path.size() == path.size()) { + continue; + } + return new_path; + } else if (edge.city2 == actual) { + vector new_path = dfs(edge.city1, v2, visited, path); + if (new_path.size() == path.size()) { + continue; + } + return new_path; + } + } + path.pop_back(); + return path; + } + + vector path(V v1, V v2) override { + vector path; + set visited; + + return dfs(v1, v2, visited, path); + } +}; + + +#endif //COMPETITIVE_PROGRAMMING_GRAPH_H diff --git a/Data_Structures/Graph/Graph_Interface.h b/Data_Structures/Graph/Graph_Interface.h new file mode 100644 index 0000000..5673c51 --- /dev/null +++ b/Data_Structures/Graph/Graph_Interface.h @@ -0,0 +1,33 @@ +// +// Created by Zener085 on 15.10.2023. +// + +#ifndef COMPETITIVE_PROGRAMMING_GRAPH_INTERFACE_H +#define COMPETITIVE_PROGRAMMING_GRAPH_INTERFACE_H + +#include + +using namespace std; + +template +class GraphInterface { + /** + * Возвращает все узлы графа + * @return Массив узлов графа + */ + virtual vector vertexes() = 0; + + /** + * Возвращает все линии графа + * @return Массив линий графа + */ + virtual vector edges() = 0; + virtual void add_vertex(V vertex) = 0; + virtual void add_edge(V v1, V v2) = 0; + virtual void remove_vertex(V vertex) = 0; + virtual void remove_edge(E edge) = 0; + + virtual vector path(V v1, V v2) = 0; +}; + +#endif //COMPETITIVE_PROGRAMMING_GRAPH_INTERFACE_H \ No newline at end of file