all done but copied solution for grid paths: dfs with prune. very tedious
This commit is contained in:
26
CSES - CSES Problem Set/Apple_Division.cpp
Normal file
26
CSES - CSES Problem Set/Apple_Division.cpp
Normal 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;
|
||||||
|
}
|
||||||
38
CSES - CSES Problem Set/Chessboard_and_Queens.cpp
Normal file
38
CSES - CSES Problem Set/Chessboard_and_Queens.cpp
Normal 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;
|
||||||
|
}
|
||||||
21
CSES - CSES Problem Set/Creating_Strings.cpp
Normal file
21
CSES - CSES Problem Set/Creating_Strings.cpp
Normal 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;
|
||||||
|
}
|
||||||
54
CSES - CSES Problem Set/Digit_Queries.cpp
Normal file
54
CSES - CSES Problem Set/Digit_Queries.cpp
Normal 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;
|
||||||
|
}
|
||||||
86
CSES - CSES Problem Set/Grid_Paths.cpp
Normal file
86
CSES - CSES Problem Set/Grid_Paths.cpp
Normal 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
|
||||||
Reference in New Issue
Block a user