done 3 sorting problems

This commit is contained in:
2024-06-03 16:57:59 +02:00
parent cf617f6dfa
commit c817cf7f3c
23 changed files with 85 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
// Apple Division
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll get_min(vector<ll> &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<ll> input(n);
for (auto &e : input) cin >> e;
cout << get_min(input, 0, 0) << endl;
return 0;
}

View File

@@ -0,0 +1,19 @@
// Bit Strings
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 1e9 + 7;
int main() {
int n; cin >> n;
ll ans = 1;
for (int i{}; i < n; i++) {
ans *= 2;
ans %= MOD;
}
ans %= MOD;
cout << ans;
return 0;
}

View File

@@ -0,0 +1,38 @@
// Chessboard and Queens
#include<bits/stdc++.h>
using namespace std;
bool is_not_attacking(vector<pair<int, int>> &p, pair<int, int> 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<string> &input, int row, vector<pair<int, int>> &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<string> input;
for (int i{}; i < 8; i++) {
string line;
cin >> line;
input.push_back(line);
}
vector<pair<int, int>> placed;
cout << queens(input, 0, placed) << endl;
return 0;
}

View File

@@ -0,0 +1,23 @@
// Coin Piles
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int t; cin >> t;
for (int T{}; T < t; T++) {
ll a, b; cin >> a >> b;
if (a < b) swap(a, b);
if ((a + b) % 3 != 0) {
cout << "NO" << endl;
continue;
} else if (a > 2*b) {
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
}
return 0;
}

View File

@@ -0,0 +1,21 @@
// Creating Strings
#include<bits/stdc++.h>
using namespace std;
int main() {
set<string> 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;
}

View File

@@ -0,0 +1,54 @@
// Digit Queries
#include<bits/stdc++.h>
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;
}

View File

@@ -0,0 +1,32 @@
// Gray Code
#include<bits/stdc++.h>
using namespace std;
vector<string> gray_code(int size, bool print) {
if (size == 1) {
if (print) {
cout << "0" << '\n' << "1" << endl;
}
return {"0", "1"};
}
auto prev_gray_code = gray_code(size - 1, false);
vector<string> output;
for (auto &s: prev_gray_code) {
if (print) cout << "0"+s << endl;
output.push_back("0"+s);
}
std::reverse(prev_gray_code.begin(), prev_gray_code.end());
for (auto &s: prev_gray_code) {
if (print) cout << "1"+s << endl;
output.push_back("1"+s);
}
return output;
}
int main() {
int n; cin >> n;
gray_code(n, true);
return 0;
}

View File

@@ -0,0 +1,86 @@
#include <iostream>
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

View File

@@ -0,0 +1,22 @@
// Increasing Array
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
vector<ll> array(n);
for (auto &e : array) cin >> e;
ll count = 0;
for (int i = 1; i < n; i++) {
if (array[i] < array[i-1]) {
count += array[i-1] - array[i];
array[i] = array[i-1];
}
}
cout << count << endl;
return 0;
}

View File

@@ -0,0 +1,23 @@
// Missing Number
#include<bits/stdc++.h>
using namespace std;
int main() {
bitset<2 * (int)1e5> input;
int n; cin >> n;
for (int i{}; i < n; i++) {
int a; cin >> a;
input[a-1] = true;
}
input = ~input;
for (int i = 0; i < n; i++) {
if (input[i]) {
cout << i + 1<< endl;
break;
}
}
return 0;
}

View File

@@ -0,0 +1,27 @@
// Number Spiral
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int t; cin >> t;
for (int i{}; i < t; i++) {
ll x, y;
cin >> y >> x;
if (y >= x) {
ll yy = y * (y-1) + 1;
if (y & 1) yy -= (y - x);
else yy += (y - x);
cout << yy << endl;
} else {
ll xx = x * (x - 1) + 1;
if (x & 1) xx += (x - y);
else xx -= (x - y);
cout << xx << endl;
}
}
return 0;
}

View File

@@ -0,0 +1,31 @@
// Palindrome Reorder
#include<bits/stdc++.h>
using namespace std;
int main() {
map<char, int> char_to_count;
string s; cin >> s;
for (auto c: s) char_to_count[c]++;
string forward;
char middle = ' ';
for (auto &[ch, c]: char_to_count) {
if (c & 1) {
if (middle != ' ') {
cout << "NO SOLUTION" << endl;
return 0;
}
middle = ch;
forward.append(string((c - 1) / 2, ch));
} else {
forward.append(string(c/2, ch));
}
}
cout << forward;
if (middle != ' ') cout << middle;
std::reverse(forward.begin(), forward.end());
cout << forward << endl;
return 0;
}

View File

@@ -0,0 +1,30 @@
// Permutations
#include<bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
if (n == 2 || n == 3) {
cout << "NO SOLUTION" << endl;
return 0;
}
if (n & 1) {
for (int i = n; i >= 1; i -= 2) {
cout << i << " ";
}
for (int i = n-1; i > 1; i -= 2) {
cout << i << " ";
}
} else {
for (int i = n - 1; i >= 1; i -= 2) {
cout << i << " ";
}
for (int i = n; i > 1; i -= 2) {
cout << i << " ";
}
}
cout << endl;
return 0;
}

View File

@@ -0,0 +1,25 @@
// Repetitions
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int max_count = 0;
int current_count = 0;
char current = ' ';
for (auto c : s) {
if (c == current) {
current_count ++;
max_count = max(max_count, current_count);
} else {
current = c;
current_count = 1;
max_count = max(max_count, current_count);
}
}
cout << max_count << endl;
return 0;
}

View File

@@ -0,0 +1,38 @@
// Tower of Hanoi
#include <bits/stdc++.h>
using namespace std;
void hanoi(deque<int> &current, deque<int> &destination, deque<int> &tmp, int n, vector<pair<int, int>> &remember, tuple<int, int, int> stack_names) {
if (n == 1) {
auto top = current.back();
current.pop_back();
destination.push_back(top);
remember.push_back({get<0>(stack_names), get<1>(stack_names)});
return;
}
hanoi(current, tmp, destination, n - 1, remember, {get<0>(stack_names), get<2>(stack_names), get<1>(stack_names)});
auto top = current.back();
current.pop_back();
destination.push_back(top);
remember.push_back({get<0>(stack_names), get<1>(stack_names)});
hanoi(tmp, destination, current, n - 1, remember, {get<2>(stack_names), get<1>(stack_names), get<0>(stack_names)});
return ;
};
int main() {
int size;
cin >> size;
deque<int> start;
deque<int> s2;
deque<int> s3;
for (int i = size; i > 0; i--) {
start.push_front(i);
}
vector<pair<int, int>> r; //c, t, d
hanoi(start, s2, s3, size, r, {1, 3, 2});
cout << r.size() << endl;
for (auto &[a, b]: r) cout << a << " " << b << endl;
cout << endl;
return 0;
}

View File

@@ -0,0 +1,16 @@
// Trailing Zeros
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int count = 0;
ll n; cin >> n;
for (int i = 1; i <= 15; i++) {
count += n / pow(5, i);
}
cout << count;
return 0;
}

View File

@@ -0,0 +1,16 @@
// Two Knights
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
for (ll i = 1; i <= n; i++) {
ll total_possibilities = i * i * (i*i - 1) / 2;
cout << total_possibilities - (4 * (i-1) * (i - 2)) << endl;
}
return 0;
}

View File

@@ -0,0 +1,28 @@
// Two Sets
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n; cin >> n;
if ((n * (n+1) / 2) & 1) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
ll target_sum = n * (n+1) / 4;
vector<ll> first;
vector<ll> second;
for (ll i = n; i > 0; i--) {
if (i <= target_sum) {first.push_back(i); target_sum -= i;}
else second.push_back(i);
}
cout << first.size() << endl;
for (auto e : first) cout << e << " ";
cout << '\n' << second.size() << '\n';
for (auto e: second) cout << e << " ";
cout << endl;
return 0;
}

View File

@@ -0,0 +1,23 @@
// Weird Algorithm
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
while (n != 1) {
cout << n << " ";
if (n & 1) {
n *= 3;
n++;
} else {
n /= 2;
}
}
cout << n << endl;
return 0;
}