diff --git a/.autocp b/.autocp index a628a29..201a240 100644 --- a/.autocp +++ b/.autocp @@ -1 +1 @@ -{"version":1,"problems":{"CSES - CSES Problem Set":{"Weird Algorithm":{"name":"Weird Algorithm","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1068/","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n","output":"3 10 5 16 8 4 2 1\n"}],"memoryLimit":512,"timeLimit":1000},"Missing Number":{"name":"Missing Number","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1083","sampleTestcases":[{"name":"Sample Testcase #1","input":"5\n2 3 1 5\n","output":"4\n"}],"memoryLimit":512,"timeLimit":1000},"Repetitions":{"name":"Repetitions","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1069","sampleTestcases":[{"name":"Sample Testcase #1","input":"ATTCGGGA\n","output":"3\n"}],"memoryLimit":512,"timeLimit":1000},"Increasing Array":{"name":"Increasing Array","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1094","sampleTestcases":[{"name":"Sample Testcase #1","input":"5\n3 2 5 1 7\n","output":"5\n"}],"memoryLimit":512,"timeLimit":1000},"Permutations":{"name":"Permutations","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1070","sampleTestcases":[{"name":"Sample Testcase #1","input":"5\n","output":"4 2 5 3 1\n"},{"name":"Sample Testcase #2","input":"3\n","output":"NO SOLUTION\n"}],"memoryLimit":512,"timeLimit":1000},"Number Spiral":{"name":"Number Spiral","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1071","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n2 3\n1 1\n4 2\n","output":"8\n1\n15\n"}],"memoryLimit":512,"timeLimit":1000},"Two Knights":{"name":"Two Knights","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1072","sampleTestcases":[{"name":"Sample Testcase #1","input":"8\n","output":"0\n6\n28\n96\n252\n550\n1056\n1848\n"}],"memoryLimit":512,"timeLimit":1000},"Two Sets":{"name":"Two Sets","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1092","sampleTestcases":[{"name":"Sample Testcase #1","input":"7\n","output":"YES\n4\n1 2 4 7\n3\n3 5 6\n"},{"name":"Sample Testcase #2","input":"6\n","output":"NO\n"}],"memoryLimit":512,"timeLimit":1000},"Bit Strings":{"name":"Bit Strings","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1617","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n","output":"8\n"}],"memoryLimit":512,"timeLimit":1000},"Trailing Zeros":{"name":"Trailing Zeros","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1618","sampleTestcases":[{"name":"Sample Testcase #1","input":"20\n","output":"4\n"}],"memoryLimit":512,"timeLimit":1000},"Coin Piles":{"name":"Coin Piles","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1754","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n2 1\n2 2\n3 3\n","output":"YES\nNO\nYES\n"}],"memoryLimit":512,"timeLimit":1000},"Palindrome Reorder":{"name":"Palindrome Reorder","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1755","sampleTestcases":[{"name":"Sample Testcase #1","input":"AAAACACBA\n","output":"AACABACAA\n"}],"memoryLimit":512,"timeLimit":1000},"Gray Code":{"name":"Gray Code","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/2205","sampleTestcases":[{"name":"Sample Testcase #1","input":"2\n","output":"00\n01\n11\n10\n"}],"memoryLimit":512,"timeLimit":1000},"Tower of Hanoi":{"name":"Tower of Hanoi","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/2165","sampleTestcases":[{"name":"Sample Testcase #1","input":"2\n","output":"3\n1 2\n1 3\n2 3\n"}],"memoryLimit":512,"timeLimit":1000}}},"solutionFiles":{"CSES - CSES Problem Set/Weird_Algorithm.cpp":{"pathString":"CSES - CSES Problem Set/Weird_Algorithm.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Weird Algorithm"},"testcases":[{"name":"Sample Testcase #1","input":"3\n","output":"3 10 5 16 8 4 2 1\n"}]},"CSES - CSES Problem Set/Missing_Number.cpp":{"pathString":"CSES - CSES Problem Set/Missing_Number.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Missing Number"},"testcases":[{"name":"Sample Testcase #1","input":"5\n2 3 1 5\n","output":"4\n"}]},"CSES - CSES Problem Set/Repetitions.cpp":{"pathString":"CSES - CSES Problem Set/Repetitions.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Repetitions"},"testcases":[{"name":"Sample Testcase #1","input":"ATTCGGGA\n","output":"3\n"}]},"CSES - CSES Problem Set/Increasing_Array.cpp":{"pathString":"CSES - CSES Problem Set/Increasing_Array.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Increasing Array"},"testcases":[{"name":"Sample Testcase #1","input":"5\n3 2 5 1 7\n","output":"5\n"}]},"CSES - CSES Problem Set/Permutations.cpp":{"pathString":"CSES - CSES Problem Set/Permutations.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Permutations"},"testcases":[{"name":"Sample Testcase #1","input":"5\n","output":"4 2 5 3 1\n"},{"name":"Sample Testcase #2","input":"3\n","output":"NO SOLUTION\n"}]},"CSES - CSES Problem Set/Number_Spiral.cpp":{"pathString":"CSES - CSES Problem Set/Number_Spiral.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Number Spiral"},"testcases":[{"name":"Sample Testcase #1","input":"3\n2 3\n1 1\n4 2\n","output":"8\n1\n15\n"}]},"CSES - CSES Problem Set/Two_Knights.cpp":{"pathString":"CSES - CSES Problem Set/Two_Knights.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Two Knights"},"testcases":[{"name":"Sample Testcase #1","input":"8\n","output":"0\n6\n28\n96\n252\n550\n1056\n1848\n"}]},"CSES - CSES Problem Set/Two_Sets.cpp":{"pathString":"CSES - CSES Problem Set/Two_Sets.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Two Sets"},"testcases":[{"name":"Sample Testcase #1","input":"7\n","output":"YES\n4\n1 2 4 7\n3\n3 5 6\n"},{"name":"Sample Testcase #2","input":"6\n","output":"NO\n"}]},"CSES - CSES Problem Set/Bit_Strings.cpp":{"pathString":"CSES - CSES Problem Set/Bit_Strings.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Bit Strings"},"testcases":[{"name":"Sample Testcase #1","input":"3\n","output":"8\n"}]},"CSES - CSES Problem Set/Trailing_Zeros.cpp":{"pathString":"CSES - CSES Problem Set/Trailing_Zeros.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Trailing Zeros"},"testcases":[{"name":"Sample Testcase #1","input":"20\n","output":"4\n"}]},"CSES - CSES Problem Set/Coin_Piles.cpp":{"pathString":"CSES - CSES Problem Set/Coin_Piles.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Coin Piles"},"testcases":[{"name":"Sample Testcase #1","input":"3\n2 1\n2 2\n3 3\n","output":"YES\nNO\nYES\n"}]},"CSES - CSES Problem Set/Palindrome_Reorder.cpp":{"pathString":"CSES - CSES Problem Set/Palindrome_Reorder.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Palindrome Reorder"},"testcases":[{"name":"Sample Testcase #1","input":"AAAACACBA\n","output":"AACABACAA\n"}]},"CSES - CSES Problem Set/Gray_Code.cpp":{"pathString":"CSES - CSES Problem Set/Gray_Code.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Gray Code"},"testcases":[{"name":"Sample Testcase #1","input":"2\n","output":"00\n01\n11\n10\n"}]},"CSES - CSES Problem Set/Tower_of_Hanoi.cpp":{"pathString":"CSES - CSES Problem Set/Tower_of_Hanoi.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Tower of Hanoi"},"testcases":[{"name":"Sample Testcase #1","input":"2\n","output":"3\n1 2\n1 3\n2 3\n"}]}}} \ No newline at end of file +{"version":1,"problems":{"CSES - CSES Problem Set":{"Weird Algorithm":{"name":"Weird Algorithm","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1068/","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n","output":"3 10 5 16 8 4 2 1\n"}],"memoryLimit":512,"timeLimit":1000},"Missing Number":{"name":"Missing Number","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1083","sampleTestcases":[{"name":"Sample Testcase #1","input":"5\n2 3 1 5\n","output":"4\n"}],"memoryLimit":512,"timeLimit":1000},"Repetitions":{"name":"Repetitions","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1069","sampleTestcases":[{"name":"Sample Testcase #1","input":"ATTCGGGA\n","output":"3\n"}],"memoryLimit":512,"timeLimit":1000},"Increasing Array":{"name":"Increasing Array","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1094","sampleTestcases":[{"name":"Sample Testcase #1","input":"5\n3 2 5 1 7\n","output":"5\n"}],"memoryLimit":512,"timeLimit":1000},"Permutations":{"name":"Permutations","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1070","sampleTestcases":[{"name":"Sample Testcase #1","input":"5\n","output":"4 2 5 3 1\n"},{"name":"Sample Testcase #2","input":"3\n","output":"NO SOLUTION\n"}],"memoryLimit":512,"timeLimit":1000},"Number Spiral":{"name":"Number Spiral","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1071","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n2 3\n1 1\n4 2\n","output":"8\n1\n15\n"}],"memoryLimit":512,"timeLimit":1000},"Two Knights":{"name":"Two Knights","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1072","sampleTestcases":[{"name":"Sample Testcase #1","input":"8\n","output":"0\n6\n28\n96\n252\n550\n1056\n1848\n"}],"memoryLimit":512,"timeLimit":1000},"Two Sets":{"name":"Two Sets","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1092","sampleTestcases":[{"name":"Sample Testcase #1","input":"7\n","output":"YES\n4\n1 2 4 7\n3\n3 5 6\n"},{"name":"Sample Testcase #2","input":"6\n","output":"NO\n"}],"memoryLimit":512,"timeLimit":1000},"Bit Strings":{"name":"Bit Strings","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1617","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n","output":"8\n"}],"memoryLimit":512,"timeLimit":1000},"Trailing Zeros":{"name":"Trailing Zeros","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1618","sampleTestcases":[{"name":"Sample Testcase #1","input":"20\n","output":"4\n"}],"memoryLimit":512,"timeLimit":1000},"Coin Piles":{"name":"Coin Piles","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1754","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n2 1\n2 2\n3 3\n","output":"YES\nNO\nYES\n"}],"memoryLimit":512,"timeLimit":1000},"Palindrome Reorder":{"name":"Palindrome Reorder","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1755","sampleTestcases":[{"name":"Sample Testcase #1","input":"AAAACACBA\n","output":"AACABACAA\n"}],"memoryLimit":512,"timeLimit":1000},"Gray Code":{"name":"Gray Code","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/2205","sampleTestcases":[{"name":"Sample Testcase #1","input":"2\n","output":"00\n01\n11\n10\n"}],"memoryLimit":512,"timeLimit":1000},"Tower of Hanoi":{"name":"Tower of Hanoi","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/2165","sampleTestcases":[{"name":"Sample Testcase #1","input":"2\n","output":"3\n1 2\n1 3\n2 3\n"}],"memoryLimit":512,"timeLimit":1000},"Creating Strings":{"name":"Creating Strings","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1622","sampleTestcases":[{"name":"Sample Testcase #1","input":"aabac\n","output":"20\naaabc\naaacb\naabac\naabca\naacab\naacba\nabaac\nabaca\nabcaa\nacaab\nacaba\nacbaa\nbaaac\nbaaca\nbacaa\nbcaaa\ncaaab\ncaaba\ncabaa\ncbaaa\n"}],"memoryLimit":512,"timeLimit":1000},"Apple Division":{"name":"Apple Division","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1623","sampleTestcases":[{"name":"Sample Testcase #1","input":"5\n3 2 7 4 1\n","output":"1\n"}],"memoryLimit":512,"timeLimit":1000},"Chessboard and Queens":{"name":"Chessboard and Queens","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1624","sampleTestcases":[{"name":"Sample Testcase #1","input":"........\n........\n..*.....\n........\n........\n.....**.\n...*....\n........\n","output":"65\n"}],"memoryLimit":512,"timeLimit":1000},"Digit Queries":{"name":"Digit Queries","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/2431","sampleTestcases":[{"name":"Sample Testcase #1","input":"3\n7\n19\n12\n","output":"7\n4\n1\n"}],"memoryLimit":512,"timeLimit":1000},"Grid Paths":{"name":"Grid Paths","groupName":"CSES - CSES Problem Set","url":"https://cses.fi/problemset/task/1625","sampleTestcases":[{"name":"Sample Testcase #1","input":"??????R??????U??????????????????????????LD????D?\n","output":"201\n"}],"memoryLimit":512,"timeLimit":1000}}},"solutionFiles":{"CSES - CSES Problem Set/Weird_Algorithm.cpp":{"pathString":"CSES - CSES Problem Set/Weird_Algorithm.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Weird Algorithm"},"testcases":[{"name":"Sample Testcase #1","input":"3\n","output":"3 10 5 16 8 4 2 1\n"}]},"CSES - CSES Problem Set/Missing_Number.cpp":{"pathString":"CSES - CSES Problem Set/Missing_Number.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Missing Number"},"testcases":[{"name":"Sample Testcase #1","input":"5\n2 3 1 5\n","output":"4\n"}]},"CSES - CSES Problem Set/Repetitions.cpp":{"pathString":"CSES - CSES Problem Set/Repetitions.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Repetitions"},"testcases":[{"name":"Sample Testcase #1","input":"ATTCGGGA\n","output":"3\n"}]},"CSES - CSES Problem Set/Increasing_Array.cpp":{"pathString":"CSES - CSES Problem Set/Increasing_Array.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Increasing Array"},"testcases":[{"name":"Sample Testcase #1","input":"5\n3 2 5 1 7\n","output":"5\n"}]},"CSES - CSES Problem Set/Permutations.cpp":{"pathString":"CSES - CSES Problem Set/Permutations.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Permutations"},"testcases":[{"name":"Sample Testcase #1","input":"5\n","output":"4 2 5 3 1\n"},{"name":"Sample Testcase #2","input":"3\n","output":"NO SOLUTION\n"}]},"CSES - CSES Problem Set/Number_Spiral.cpp":{"pathString":"CSES - CSES Problem Set/Number_Spiral.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Number Spiral"},"testcases":[{"name":"Sample Testcase #1","input":"3\n2 3\n1 1\n4 2\n","output":"8\n1\n15\n"}]},"CSES - CSES Problem Set/Two_Knights.cpp":{"pathString":"CSES - CSES Problem Set/Two_Knights.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Two Knights"},"testcases":[{"name":"Sample Testcase #1","input":"8\n","output":"0\n6\n28\n96\n252\n550\n1056\n1848\n"}]},"CSES - CSES Problem Set/Two_Sets.cpp":{"pathString":"CSES - CSES Problem Set/Two_Sets.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Two Sets"},"testcases":[{"name":"Sample Testcase #1","input":"7\n","output":"YES\n4\n1 2 4 7\n3\n3 5 6\n"},{"name":"Sample Testcase #2","input":"6\n","output":"NO\n"}]},"CSES - CSES Problem Set/Bit_Strings.cpp":{"pathString":"CSES - CSES Problem Set/Bit_Strings.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Bit Strings"},"testcases":[{"name":"Sample Testcase #1","input":"3\n","output":"8\n"}]},"CSES - CSES Problem Set/Trailing_Zeros.cpp":{"pathString":"CSES - CSES Problem Set/Trailing_Zeros.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Trailing Zeros"},"testcases":[{"name":"Sample Testcase #1","input":"20\n","output":"4\n"}]},"CSES - CSES Problem Set/Coin_Piles.cpp":{"pathString":"CSES - CSES Problem Set/Coin_Piles.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Coin Piles"},"testcases":[{"name":"Sample Testcase #1","input":"3\n2 1\n2 2\n3 3\n","output":"YES\nNO\nYES\n"}]},"CSES - CSES Problem Set/Palindrome_Reorder.cpp":{"pathString":"CSES - CSES Problem Set/Palindrome_Reorder.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Palindrome Reorder"},"testcases":[{"name":"Sample Testcase #1","input":"AAAACACBA\n","output":"AACABACAA\n"}]},"CSES - CSES Problem Set/Gray_Code.cpp":{"pathString":"CSES - CSES Problem Set/Gray_Code.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Gray Code"},"testcases":[{"name":"Sample Testcase #1","input":"2\n","output":"00\n01\n11\n10\n"}]},"CSES - CSES Problem Set/Tower_of_Hanoi.cpp":{"pathString":"CSES - CSES Problem Set/Tower_of_Hanoi.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Tower of Hanoi"},"testcases":[{"name":"Sample Testcase #1","input":"2\n","output":"3\n1 2\n1 3\n2 3\n"}]},"CSES - CSES Problem Set/Creating_Strings.cpp":{"pathString":"CSES - CSES Problem Set/Creating_Strings.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Creating Strings"},"testcases":[{"name":"Sample Testcase #1","input":"aabac\n","output":"20\naaabc\naaacb\naabac\naabca\naacab\naacba\nabaac\nabaca\nabcaa\nacaab\nacaba\nacbaa\nbaaac\nbaaca\nbacaa\nbcaaa\ncaaab\ncaaba\ncabaa\ncbaaa\n"}]},"CSES - CSES Problem Set/Apple_Division.cpp":{"pathString":"CSES - CSES Problem Set/Apple_Division.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Apple Division"},"testcases":[{"name":"Sample Testcase #1","input":"5\n3 2 7 4 1\n","output":"1\n"}]},"CSES - CSES Problem Set/Chessboard_and_Queens.cpp":{"pathString":"CSES - CSES Problem Set/Chessboard_and_Queens.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Chessboard and Queens"},"testcases":[{"name":"Sample Testcase #1","input":"........\n........\n..*.....\n........\n........\n.....**.\n...*....\n........\n","output":"65\n"}]},"CSES - CSES Problem Set/Digit_Queries.cpp":{"pathString":"CSES - CSES Problem Set/Digit_Queries.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Digit Queries"},"testcases":[{"name":"Sample Testcase #1","input":"3\n7\n19\n12\n","output":"7\n4\n1\n"}]},"CSES - CSES Problem Set/Grid_Paths.cpp":{"pathString":"CSES - CSES Problem Set/Grid_Paths.cpp","linkedProblemId":{"first":"CSES - CSES Problem Set","second":"Grid Paths"},"testcases":[{"name":"Sample Testcase #1","input":"??????R??????U??????????????????????????LD????D?\n","output":"201\n"}]}}} \ No newline at end of file diff --git a/CSES - CSES Problem Set/Apple_Division.cpp b/CSES - CSES Problem Set/Apple_Division.cpp new file mode 100644 index 0000000..fd15f45 --- /dev/null +++ b/CSES - CSES Problem Set/Apple_Division.cpp @@ -0,0 +1,26 @@ +// Apple Division + +#include + +using namespace std; +using ll = long long; + +ll get_min(vector &p, int idx, ll sum) { + if (idx == p.size()) { + ll total_sum = 0; + for (auto e: p) total_sum += e; + sum *= 2; + return abs(sum - total_sum); + } + ll with_idx = get_min(p, idx+1, sum+p[idx]); + ll without_idx = get_min(p, idx+1, sum); + return min(with_idx, without_idx); +} + +int main() { + int n; cin >> n; + vector input(n); + for (auto &e : input) cin >> e; + cout << get_min(input, 0, 0) << endl; + return 0; +} diff --git a/CSES - CSES Problem Set/Chessboard_and_Queens.cpp b/CSES - CSES Problem Set/Chessboard_and_Queens.cpp new file mode 100644 index 0000000..e5ce00d --- /dev/null +++ b/CSES - CSES Problem Set/Chessboard_and_Queens.cpp @@ -0,0 +1,38 @@ +// Chessboard and Queens + +#include + +using namespace std; + +bool is_not_attacking(vector> &p, pair n) { + for (auto &[x, y]: p) { + if (x == n.first || y == n.second) return false; + if (abs(x - n.first) == abs(y - n.second)) return false; + } + return true; +} + +int queens(vector &input, int row, vector> &placed) { + if (row == input.size()) return 1; + int possibilities = 0; + for (int i{}; i < 8; i++) { + if (input[row][i] != '*' && is_not_attacking(placed, {i, row})) { + placed.push_back({i, row}); + possibilities += queens(input, row + 1, placed); + placed.pop_back(); + } + } + return possibilities; +} + +int main() { + vector input; + for (int i{}; i < 8; i++) { + string line; + cin >> line; + input.push_back(line); + } + vector> placed; + cout << queens(input, 0, placed) << endl; + return 0; +} diff --git a/CSES - CSES Problem Set/Creating_Strings.cpp b/CSES - CSES Problem Set/Creating_Strings.cpp new file mode 100644 index 0000000..9e50dec --- /dev/null +++ b/CSES - CSES Problem Set/Creating_Strings.cpp @@ -0,0 +1,21 @@ +// Creating Strings + +#include + +using namespace std; + +int main() { + set combinations; + string s; + cin >> s; + sort(s.begin(), s.end()); + do { + combinations.insert(s); + } while (next_permutation(s.begin(), s.end())); + combinations.insert(s); + cout << combinations.size() << endl; + for(auto p: combinations) { + cout << p << endl; + } + return 0; +} diff --git a/CSES - CSES Problem Set/Digit_Queries.cpp b/CSES - CSES Problem Set/Digit_Queries.cpp new file mode 100644 index 0000000..dc08401 --- /dev/null +++ b/CSES - CSES Problem Set/Digit_Queries.cpp @@ -0,0 +1,54 @@ +// Digit Queries + +#include + +using namespace std; +using ll = long long; + + +long long int power(ll base, ll exp) +{ + if (exp == 0) + return 1; + else if (exp == 1) + return base; + else + { + long long int calc; + if (exp % 2 == 0) + { + calc = power(base, exp/2); + calc *= calc; + } + else + { + calc = base*power(base, exp-1); + } + return calc; + } +} + + +int main() { + ll q; cin >> q; + for (ll _{}; _ < q; _++) { + ll k; cin >> k; + if (k < 10) {cout << k << endl; continue;} + ll number_length = 1; + ll highest_idx = 0; + while (highest_idx < k) { + highest_idx += number_length * 9 * power(10, number_length-1); + number_length++; + } + number_length--; + ll pos = k - highest_idx + number_length * 9 * power(10, number_length - 1) - 1; + ll number = pos / (number_length); + number += power(10, number_length - 1); +// cout << number << " " << number_length << endl; + + ll decimal_pos = pos % number_length; + ll out = (number / (power(10, number_length - decimal_pos - 1))) % 10 ; + cout << out << endl; + } + return 0; +} diff --git a/CSES - CSES Problem Set/Grid_Paths.cpp b/CSES - CSES Problem Set/Grid_Paths.cpp new file mode 100644 index 0000000..75dc053 --- /dev/null +++ b/CSES - CSES Problem Set/Grid_Paths.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +const int DIR_LEN = 4; +int dr[DIR_LEN] = {-1, 0, 1, 0}; +int dc[DIR_LEN] = {0, 1, 0, -1}; +const int PATH_LEN = 48; // length of all possible paths +int p[PATH_LEN]; +const int GRID_SIZE = 9; +// added border to all four sides so a 7x7 becomes a 9x9 +bool onPath[GRID_SIZE][GRID_SIZE]; + +int tryPath(int pathIdx, int curR, int curC) { + // Optimization 3 + if ((onPath[curR][curC - 1] && onPath[curR][curC + 1]) && + (!onPath[curR - 1][curC] && !onPath[curR + 1][curC])) + return 0; + if ((onPath[curR - 1][curC] && onPath[curR + 1][curC]) && + (!onPath[curR][curC - 1] && !onPath[curR][curC + 1])) + return 0; + + if (curR == 7 && curC == 1) { // reached endpoint before visiting all + if (pathIdx == PATH_LEN) return 1; + return 0; + } + + if (pathIdx == PATH_LEN) return 0; + + int ret = 0; + onPath[curR][curC] = true; + + // turn already determined: + if (p[pathIdx] < 4) { + int nxtR = curR + dr[p[pathIdx]]; + int nxtC = curC + dc[p[pathIdx]]; + if (!onPath[nxtR][nxtC]) ret += tryPath(pathIdx + 1, nxtR, nxtC); + } + // see Java solution for optimization 4 implementation + else { // iterate through all four possible turns + for (int i = 0; i < DIR_LEN; i++) { + int nxtR = curR + dr[i]; + int nxtC = curC + dc[i]; + if (onPath[nxtR][nxtC]) continue; + ret += tryPath(pathIdx + 1, nxtR, nxtC); + } + } + // reset and return + onPath[curR][curC] = false; + return ret; +} + +int main() { + string line; + getline(cin, line); + + // convert path to ints + for (int i = 0; i < PATH_LEN; i++) { + char cur = line[i]; + + if (cur == 'U') p[i] = 0; + else if (cur == 'R') p[i] = 1; + else if (cur == 'D') p[i] = 2; + else if (cur == 'L') p[i] = 3; + else p[i] = 4; // cur == '?' + } + + // set borders of grid + for (int i = 0; i < GRID_SIZE; i++) { + onPath[0][i] = true; + onPath[8][i] = true; + onPath[i][0] = true; + onPath[i][8] = true; + } + // initialize the inside of the grid to be completely empty + for (int i = 1; i <= 7; i++) { + for (int j = 1; j <= 7; j++) { onPath[i][j] = false; } + } + + int startIdx = 0; + int startR = 1; + int startC = 1; // always start path at (1, 1) + int ans = tryPath(startIdx, startR, startC); + cout << ans << endl; +} + +//https://usaco.guide/problems/cses-1625-grid-paths/solution \ No newline at end of file