diff --git a/2003/S3.cpp b/2003/S3.cpp new file mode 100644 index 0000000..2e88adf --- /dev/null +++ b/2003/S3.cpp @@ -0,0 +1,61 @@ +/* + * Author: DynamicSquid + */ + +#include +#include + +int cmp(const void* a, const void* b) { + return *((int*)a) < *((int*)b); +} + +int r, c; +char grid[25][26]; + +int rooms_size = 0; +int rooms[10] = { 0 }; + +void flood(int x, int y) { + if (x < 0 || x >= c || y < 0 || y >= r || grid[y][x] == 'I') + return; + grid[y][x] = 'I'; + + flood(x + 1, y); + flood(x - 1, y); + flood(x, y + 1); + flood(x, y - 1); + + rooms[rooms_size]++; +} + +int main() { + int flooring; + scanf("%i %i %i", &flooring, &r, &c); + + for (int a = 0; a < r; ++a) + scanf("%s", grid[a]); + + for (int a = 0; a < r; ++a) { + for (int b = 0; b < c; ++b) { + if (grid[a][b] == '.') { + flood(b, a); + rooms_size++; + } + } + } + + qsort(rooms, rooms_size, sizeof(int), cmp); + + int ans = 0; + for (int i = 0; i < rooms_size; ++i, ++ans) { + if (flooring - rooms[i] < 0) + break; + + flooring -= rooms[i]; + } + + if (ans == 1) + printf("1 room, %i square metre(s) left over", flooring); + else + printf("%i rooms, %i square metre(s) left over", ans, flooring); +} \ No newline at end of file diff --git a/2013/S3.cpp b/2013/S3.cpp new file mode 100644 index 0000000..aeef0ce --- /dev/null +++ b/2013/S3.cpp @@ -0,0 +1,87 @@ +/* + * Author: DynamicSquid + */ + +#include +#include +using namespace std; + +int T, wins = 0; + +void score(vector > games, vector points) { + if (games.empty()) { + int max = 0; + for (size_t a = 1; a < points.size(); ++a) { + if (points[a] > points[max]) + max = a; + } + + for (size_t a = 0; a < points.size(); ++a) { + if (a != max && points[a] == points[max]) + return; + } + + if (max == T - 1) + wins++; + + return; + } + + int t1 = games.back()[0] - 1; + int t2 = games.back()[1] - 1; + games.pop_back(); + + points[t1] += 3; + score(games, points); + points[t1] -= 3; + + points[t2] += 3; + score(games, points); + points[t2] -= 3; + + points[t1]++; + points[t2]++; + score(games, points); + points[t1]--; + points[t2]--; +} + +int main() { + int G; + cin >> T >> G; + + vector > games{ + { 1, 2 }, { 1, 3 }, { 1, 4 }, + { 2, 3 }, { 2, 4 }, + { 3, 4 } + }; + + vector points{ 0, 0, 0, 0 }; + + for (int a = 0; a < G; ++a) { + int t1, t2, s1, s2; + cin >> t1 >> t2 >> s1 >> s2; + + for (size_t b = 0; b < games.size(); ++b) { + if ((games[b][0] == t1 && games[b][1] == t2) || (games[b][0] == t2 && games[b][1] == t1)) { + games.erase(games.begin() + b); + break; + } + } + + if (s1 > s2) { + points[t1 - 1] += 3; + } + else if (s1 < s2) { + points[t2 - 1] += 3; + } + else { + points[t1 - 1]++; + points[t2 - 1]++; + } + } + + score(games, points); + + cout << wins; +} diff --git a/2016/J3.cpp b/2016/J3.cpp new file mode 100644 index 0000000..49bcfba --- /dev/null +++ b/2016/J3.cpp @@ -0,0 +1,31 @@ +/* + * Author: DynamicSquid + */ + +#include +using namespace std; + +bool palindrome(string& S) { + for (int a = 0; a < (int)S.length() / 2; ++a) { + if (S[a] != S[S.length() - a - 1]) + return false; + } + + return true; +} + +int main() { + string S; + cin >> S; + + int longest = 1; + for (int a = 2; a <= (int)S.length(); ++a) { + for (int b = 0; b <= (int)S.length() - a; ++b) { + string sub = S.substr(b, a); + if (palindrome(sub) && (int)sub.length() > longest) + longest = sub.length(); + } + } + + cout << longest; +} diff --git a/2017/J4.cpp b/2017/J4.cpp new file mode 100644 index 0000000..de1d9a7 --- /dev/null +++ b/2017/J4.cpp @@ -0,0 +1,36 @@ +/* + * Author: DynamicSquid + */ + +#include +using namespace std; + +int main() { + int D; + cin >> D; + + int repeat = D / 1440; + D %= 1440; + + int t = 1200, ans = 0; + for (int a = 0; a < D; ++a) { + t++; + + if (t % 100 == 60) + t = (t + 100) / 100 * 100; + if (t / 100 == 13) + t = (t % 100) + 100; + + int d1 = t % 10; + int d2 = (t / 10) % 10; + int d3 = (t / 100) % 10; + int d4 = t / 1000; + + if (d4 != 0 && d4 - d3 == d3 - d2 && d3 - d2 == d2 - d1) + ans++; + else if (d4 == 0 && d3 - d2 == d2 - d1) + ans++; + } + + cout << ans + (repeat * 62); +} diff --git a/2018/J5.cpp b/2018/J5.cpp new file mode 100644 index 0000000..740d134 --- /dev/null +++ b/2018/J5.cpp @@ -0,0 +1,65 @@ +/* + * Author: DynamicSquid + */ + +#include +#include +#include +using namespace std; + +vector nums[10005]; +bool reach[10005] = { true, false }; + +void can_reach(int i) { + for (int num : nums[i]) { + if (reach[num]) + continue; + + reach[num] = true; + can_reach(num); + } +} + +int main() { + int N; + cin >> N; + + for (int a = 0; a < N; ++a) { + int M; + cin >> M; + + nums[a].resize(M); + for (int b = 0; b < M; ++b) { + int num; + cin >> num; + nums[a][b] = num - 1; + } + } + + can_reach(0); + + bool reachable = true; + for (int a = 0; a < N && reachable; ++a) { + if (!reach[a]) + reachable = false; + } + + cout << (reachable ? 'Y' : 'N') << '\n'; + + queue > q; + q.push({ 0, 1 }); + + while (!q.empty()) { + int i = q.front()[0]; + int s = q.front()[1]; + q.pop(); + + if (nums[i].empty()) { + cout << s; + break; + } + + for (int num : nums[i]) + q.push({ num, s + 1 }); + } +} diff --git a/2019/J3.cpp b/2019/J3.cpp new file mode 100644 index 0000000..c96b5c4 --- /dev/null +++ b/2019/J3.cpp @@ -0,0 +1,36 @@ +/* + * Author: DynamicSquid + */ + +#include +#include +#include +using namespace std; + +int main() { + int num; + cin >> num; + + vector lines(num); + for (int a = 0; a < num; ++a) + cin >> lines[a]; + + for (size_t a = 0; a < lines.size(); ++a) { + char letter = lines[a][0]; + int count = 0; + + for (size_t b = 0; b < lines[a].length(); ++b) { + if (lines[a][b] == letter) { + count++; + } + else { + cout << count << ' ' << letter << ' '; + + letter = lines[a][b]; + count = 1; + } + } + + cout << count << ' ' << letter << '\n'; + } +} diff --git a/2020/J3.cpp b/2020/J3.cpp new file mode 100644 index 0000000..66bdd75 --- /dev/null +++ b/2020/J3.cpp @@ -0,0 +1,41 @@ +/* + * Author: DynamicSquid + */ + +#include +using namespace std; + +int main() { + int N; + cin >> N; + + int right = 0, left = 0, top = 0, bottom = 0; + + for (int a = 0; a < N; ++a) { + int inX, inY; + cin >> inX; + cin.ignore(1); + cin >> inY; + + if (a == 0) { + right = inX + 1; + left = inX - 1; + top = inY + 1; + bottom = inY - 1; + } + else { + if (inX <= left) + left = inX - 1; + else if (inX >= right) + right = inX + 1; + + if (inY >= top) + top = inY + 1; + else if (inY <= bottom) + bottom = inY - 1; + } + } + + cout << left << "," << bottom << "\n"; + cout << right << "," << top; +} diff --git a/2020/J5-S2.cpp b/2020/J5-S2.cpp new file mode 100644 index 0000000..f0ee032 --- /dev/null +++ b/2020/J5-S2.cpp @@ -0,0 +1,60 @@ +/* + * Author: DynamicSquid + */ + +#include +#include +using namespace std; + +struct Cell { + int val; + bool visit; +}; + +void jump(vector >& grid, int x, int y) { + if (x == grid[0].size() - 1 && y == grid.size() - 1) + throw 0; + + int num = grid[y][x].val; + grid[y][x].visit = true; + + for (int a = 1; a * a <= num; ++a) { + if (num % a == 0) { + int num1 = a; + int num2 = num / a; + + if (num2 <= grid.size() && num1 <= grid[0].size() && !grid[num2 - 1][num1 - 1].visit) { + grid[num2 - 1][num1 - 1].visit = true; + jump(grid, num1 - 1, num2 - 1); + } + if (num2 <= grid[0].size() && num1 <= grid.size() && !grid[num1 - 1][num2 - 1].visit) { + grid[num1 - 1][num2 - 1].visit = true; + jump(grid, num2 - 1, num1 - 1); + } + } + } +} + +int main() { + int M, N; + cin >> M >> N; + + vector > grid(M); + for (int a = 0; a < M; ++a) { + for (int b = 0; b < N; ++b) { + int num; + cin >> num; + grid[a].push_back({ num, false }); + } + } + + grid[0][0].visit = true; + + try { + jump(grid, 0, 0); + cout << "no"; + } + catch (...) { + cout << "yes"; + } +} diff --git a/2020/S1.cpp b/2020/S1.cpp new file mode 100644 index 0000000..08f3f8e --- /dev/null +++ b/2020/S1.cpp @@ -0,0 +1,40 @@ +/* + * Author: DynamicSquid + */ + +#include +#include +#include +#include +using namespace std; + +int main() { + int N; + cin >> N; + + vector > nums(N); + for (int a = 0; a < N; ++a) { + double x, y; + cin >> x >> y; + nums[a] = { x, y }; + } + + sort(nums.begin(), nums.end(), [](vector& one, vector& two){ + return one[0] > two[0]; + }); + + double time = abs(nums[0][0] - nums[1][0]); + double dist = abs(nums[0][1] - nums[1][1]); + + for (size_t a = 1; a < nums.size() - 1; ++a) { + double t = abs(nums[a][0] - nums[a + 1][0]); + double d = abs(nums[a][1] - nums[a + 1][1]); + + if (d / t > dist / time) { + dist = d; + time = t; + } + } + + cout << fixed << setprecision(5) << dist / time; +}